In the following javascript code there is []
being assigned as the value of a variable, what does it mean?
var openTollDebug = [];
{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.
The reason for [] == false even though [] is truthy is: the comparison [] == false compares the value of [] to false . And to get the value of [] , the JavaScript engine first calls []. toString() . That results in "" , and that is what's actually compared to false .
[] is declaring an array. {} is declaring an object. An array has all the features of an object with additional features (you can think of an array like a sub-class of an object) where additional methods and capabilities are added in the Array sub-class.
The [] operator converts the expression inside the square brackets to a string. For instance, if it is a numeric value, JavaScript converts it to a string and then uses that string as the property name, similar to the square bracket notation of objects to access their properties.
it is an array literal. It is not quite the same as declaring new Array()
- the Array object can be overwritten in JavaScript, but the array literal can't. Here's an example to demonstrate
// let's overwrite the Array object Array = function(id) { this.id = id; } var a = new Array(1); var b = []; console.log(a.hasOwnProperty("id")); // true console.log(b.hasOwnProperty("id")); // false console.log(a.push); // false, push doesn't exist on a console.log(b.push); // true, but it does on b b.push(2); console.log(b); // outputs [2]
It means an array.
var openTollDebug = [];
declares the openTollDebug
variable and initializes it to an empty array. To put elements into the array you could do the following:
var stringArray = ['element1', 'element2', 'element3']; alert(stringArray[1]); // displays 'element2' var numberArray = [1, 2, 3, 4]; alert(numberArray[2]); // displays 3 var objectArray = [{ name: 'john' }, { name: 'peter' }, { name: 'tom' }]; alert(objectArray[1].name); // displays 'peter'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With