What's wrong with var x = new Array();

Asked
Active3 hr before
Viewed126 times

9 Answers

arraywrong
90%

In other words, [] is unambiguous., If you override Array, what happens to var x = []; That is an Array object is it not? – Chris S May 20 '09 at 22:09 ,It's safer to use [] than it is to use new Array(), because you can actually override the value of Array in JavaScript:,http://jsperf.com/new-array-vs-literal/11 ( [1,.2] vs new Array(1,.2) ) the literal is way faster in this circumstance.

It's safer to use [] than it is to use new Array(), because you can actually override the value of Array in JavaScript:

Array = function() {};

var x = new Array();
// x is now an Object instead of an Array.
88%

(That's not a real variable name) should be,In JSLint, it warns that,What is wrong with the 1st syntax? What's the reasoning behind the suggestion?,var s='' is not more correct than var s=new String()....

In JSLint, it warns that

var x = new Array();

(That's not a real variable name) should be

var result = [];
load more v
72%

This only happens when you give only one integer to the Array constructor. Why, what it's doing? Well, the new Array constructor is taking a page from some programming languages where you needed to specify the memory for your array so you don't get those ArrayIndexOutOfBounds Exceptions.,@jjperezaguinaga are you sure that the Array constructor doesn't return Array instances?,For instance, take this tail recursion implementation of Array.prototype.map,I love javascript, but sometimes it seems it doesn't love me back. Take for instance the Array constructor function.

I love javascript, but sometimes it seems it doesn't love me back. Take for instance the Array constructor function.

var a = new Array(1, 2, 3, 4, 5);
a[0] // returns 1
a.length // returns 5

Wanna take a shot in what this returns?

var a = new Array(10);
a[0] // returns undefined
a.length // returns 10, because of reasons.

This only happens when you give only one integer to the Array constructor. Why, what it's doing? Well, the new Array constructor is taking a page from some programming languages where you needed to specify the memory for your array so you don't get those ArrayIndexOutOfBounds Exceptions.

int * a = (int * ) malloc(10 * sizeof(int)); // ya ol' c
int * a = new int[10]; // c++
int[] a = new int[10]; // java

Yep, it's actually creating an array of with length of 10. We don't have a sizeof function in Javascript, but a toString is enough to prove it.

a.toString() // returns ",,,,,,,,,", a comma for each element + 1

You may think that it is correct "semantics", and it is. How else would you create an array of X length? The catch here is, that I've never needed to define a size in an array, since Javascript doesn't need to allocate memory for an array. They are more like... ArrayLists, if you want to go all Computer Science on it. For instance, all the following code is valid in Javascript:

a.pop(); // returns last object of array, removes it
a.push(20); // puts 20 as the last element of the array, returns length
a.shift(); // returns first object of the array, removes it
a.unshift(30); // puts 30 as the first element of the array, returns length

Using new Array[int] doesn't really have any sense, use, and can prompt to confusion in Javascript. Why? Because native methods of javascript think you know this. For instance, imagine this.

var a = new Array(10);
a.push(30); // returns 11, which if it were a proper stack/queue it would return 10
a.toString() // returns ",,,,,,,,,,20", probably not what you want

We should always use the [] constructor to avoid this kind of things.

var a = [10]
a.toString() // returns "10"
a[0] // returns 10
a.length // returns 1
load more v
65%

(That's not a real variable name) should be,In JSLint, it warns that,What is wrong with the 1st syntax? What's the reasoning behind the suggestion?,Is what is causing the error.

In JSLint, it warns that

var x = new Array();

(That's not a real variable name) should be

var result = [];
load more v
75%

A traditional, old-browser-compliant solution is to simply save your reference to this in a variable that can then be inherited by the closure; e.g.:,Memory Leak Example 1: Dangling references to defunct objects,A surprisingly high percentage of JavaScript developers fail to fully understand, and therefore to fully leverage, the features of prototypal inheritance.,Alternatively, in newer browsers, you can use the bind() method to pass in the proper reference:

Consider this example code snippet:

Game.prototype.restart = function() {
   this.clearLocalStorage();
   this.timer = setTimeout(function() {
      this.clearBoard(); // what is "this"?
   }, 0);
};
load more v
40%

The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects.,From the JavaScript Guide: “Indexing object properties” “Indexed collections: Array object” ,The constructor function is used to create derived objects.,Returns a new array containing the results of calling a function on every element in this array.

let fruits = ['Apple', 'Banana']

console.log(fruits.length)
// 2
load more v
22%

This chapter points out some common JavaScript mistakes.,This if statement returns false (as expected) because x is not equal to 10:,This if statement returns false (maybe not as expected), because 0 is false:,This if statement returns true (maybe not as expected), because 10 is true:

Accidentally Using the Assignment Operator

JavaScript programs may generate unexpected results if a programmer accidentally uses an assignment operator (=), instead of a comparison operator (==) in an if statement.

=

Accidentally Using the Assignment Operator

JavaScript programs may generate unexpected results if a programmer accidentally uses an assignment operator (=), instead of a comparison operator (==) in an if statement.

==
load more v
60%

Symbol - It is a new data type introduced in the ES6 version of javascript. It is used to store an anonymous and unique value. , Undefined - When a variable is declared but not assigned, it has the value of undefined and it’s type is also undefined.,Javascript Interview Questions,Typescript Interview Questions

 var str = "Vivek Singh Bisht"; //using double quotes
 var str2 = 'John Doe'; //using single quotes
load more v
48%

2) Which of the following is the correct output for the following JavaScript code:,12) Which of the following is the correct output for the following JavaScript code:,55) Which of the following is the correct output for the following JavaScript code:,39) Which of the following is the correct output for the following JavaScript code:

True
False
load more v

Other "array-wrong" queries related to "What's wrong with var x = new Array();"