Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9039875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:50:21+00:00 2026-06-16T09:50:21+00:00

var x = new Array(10); $.inArray(10,x); #Returns -1 Fiddle I’ve come across this weird

  • 0
var x = new Array(10);
$.inArray(10,x);
#Returns -1

Fiddle

I’ve come across this weird issue, checking a value in an array with $.inArray, really simple.

But, if the array only has one value in it, inArray returns -1.
If I add another value to the array, it works as expected.

This only happens with integers and not with strings.

What’s going on?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:50:22+00:00Added an answer on June 16, 2026 at 9:50 am

    If you want to create an array with the one number(10) inside you should use bracket literal:

    var x = [10];
    $.inArray(10,x);
    

    Or with push:

    var x = new Array();
    x.push(10);
    

    Obviously the first one is more readable and faster to write.

    A JavaScript array is initialized with the given elements, except in the case where a single argument is passed to the Array constructor and that argument is a number. Note that this special case only applies to JavaScript arrays created with the Array constructor, not with array literals created with the bracket syntax.

    If the only argument passed to the Array constructor is an integer, a new, empty JavaScript array and its length is set to that number
    MDN

    Fixed fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array created with this code: var widthRange = new Array(); widthRange[46]
So I have this array: var statuses = { New:1, Addressed:2, Rejected:3, Recovered:4, Billed:5,
var UserBoard = new Array(20,20); for(var i = 0; i < 21; ++i){ for(var
var grossBrackets = new Array( '300', '400', '500', '600', '700', '800', '900', '1000' );
var pieceSize = new Array(); pieceSize[0] = 200; pieceSize[1] = 400; pieceSize[2] = 300;
Given var myHash = new Array(); myHash['key1'] = { Name: 'Object 1' }; myHash['key2']
I have an array in Javascript: var array = new array(); array[0] = apples;
Let's say I have an array var test = new Array() the values in
Lets say I have the following array in JavaScript: var skins = new Array('Light',
I would like to define the following in Javascript: var tileXml = new Array();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.