What is undefined x 1 in JavaScript?
That seems to be Chrome's new way of displaying uninitialized indexes in arrays (and array-like objects):
> Array(100)
[undefined × 100]
Which is certainly better than printing [undefined, undefined, undefined,...]
or however it was before.
Although, if there is only one undefined
value, they could drop the x 1
.
What is the difference between [undefined] and [,]?
[,]
is a sparse array. It has a length of 1
, but no values (0 in [,] === false
). It can also be written as new Array(1)
.
[undefined]
is an array of length 1
with the value undefined
at index 0
.
When accessing the property "0
", both will return undefined
- the first because that property is not defined, the second because the value is "undefined". However, the arrays are different, and so is their output in the console.
Why is x undefined in inner scope?
Variable hoisting. The actual code is executed like this.
var x = 1;
(function() {
var x; // x = undefined
console.log(x);
x = 2;
})();
Edit: On Mr Lister's advice, a bit on variable hoisting. From MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var):
"Variable declarations, wherever they occur, are processed before any code is executed. The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global."
javascript array empty and undefined
When you read a property which doesn’t exist you get the value undefined
. That’s standard JS.
When you log a whole array, you aren’t reading the property explicitly, so the console helpfully distinguishes between “has no value” and “explicitly has the undefined
value”.
Why does (x += x += 1) evaluate differently in C and Javascript?
JavaScript and Java have pretty much strict left-to-right evaluation rules for this expression. C does not (even in the version you provided that has the identity function intervening).
The ECMAScript spec I have (3rd Edition, which I'll admit is quite old – the current version can be found here: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf) says that compound assignment operators are evaluated like so:
11.13.2 Compound Assignment ( op= )
The production AssignmentExpression : LeftHandSideExpression @ =
AssignmentExpression, where@ represents one of the operators indicated
above, is evaluated as follows:
- Evaluate LeftHandSideExpression.
- Call GetValue(Result(1)).
- Evaluate AssignmentExpression.
- Call GetValue(Result(3)).
- Apply operator @ to Result(2) and Result(4).
- Call PutValue(Result(1), Result(5)).
- Return Result(5)
You note that Java has the same behavior as JavaScript – I think its spec is more readable, so I'll post some snippets here (http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7):
15.7 Evaluation Order
The Java programming language guarantees that the operands of
operators appear to be evaluated in a specific evaluation order,
namely, from left to right.It is recommended that code not rely crucially on this specification.
Code is usually clearer when each expression contains at most one side
effect, as its outermost operation, and when code does not depend on
exactly which exception arises as a consequence of the left-to-right
evaluation of expressions.15.7.1 Evaluate Left-Hand Operand First The left-hand operand of a binary operator appears to be fully evaluated before any part of the
right-hand operand is evaluated. For example, if the left-hand operand
contains an assignment to a variable and the right-hand operand
contains a reference to that same variable, then the value produced by
the reference will reflect the fact that the assignment occurred
first....
If the operator is a compound-assignment operator (§15.26.2), then
evaluation of the left-hand operand includes both remembering the
variable that the left-hand operand denotes and fetching and saving
that variable's value for use in the implied combining operation.
On the other hand, in the not-undefined-behavior example where you provide an intermediate identity function:
x += id(x += 1);
while it's not undefined behavior (since the function call provides a sequence point), it's still unspecified behavior whether the leftmost x
is evaluated before the function call or after. So, while it's not 'anything goes' undefined behavior, the C compiler is still permitted to evaluate both x
variables before calling the id()
function, in which case the final value stored to the variable will be 1
:
For example, if x == 0
to start, the evaluation could look like:
tmp = x; // tmp == 0
x = tmp + id( x = tmp + 1)
// x == 1 at this point
or it could evaluate it like so:
tmp = id( x = x + 1); // tmp == 1, x == 1
x = x + tmp;
// x == 2 at this point
Note that unspecified behavior is subtly different than undefined behavior, but it's still not desirable behavior.
What is the difference between null and undefined in JavaScript?
undefined
means a variable has been declared but has not yet been assigned a value :
var testVar;
alert(testVar); //shows undefined
alert(typeof testVar); //shows undefined
Array.from method return an array with undefined values instead of real numbers
You need to return the value, you are just incrementing it and simply not returning it, hence undefined
is added.
const result = Array.from({ length: 7 }, (curr, i) => i + 1 );
console.log(result)
Related Topics
How to Access the Contents of an Svg File in an <Img> Element
How to Create a Custom Scrollbar on a Div (Facebook Style)
Google Maps: How to Create a Custom Infowindow
How to Detect a Long Touch Pressure with JavaScript for Android and Iphone
How to Exchange Variables Between Two HTML Pages
How to Draw a Line Between Two Divs
Casperjs/Phantomjs Doesn't Load Https Page
How to Disable a Href Link in JavaScript
Uploading Multiple Files to Google Drive with Google App Script
How to Fix Navigator/Window/Document Is Undefined in Nuxt
Drop-Down Box Dependent on the Option Selected in Another Drop-Down Box
How to Add and Remove Classes in JavaScript Without Jquery
Change Text Color Based on Brightness of the Covered Background Area
How to Get Text of an Input Text Box During Onkeypress
Split a String by Commas But Ignore Commas Within Double-Quotes Using JavaScript