Non optional semicolons in JavaScript? [duplicate] - javascript

This question already has answers here:
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
Semicolon before square bracket
(2 answers)
Closed 7 months ago.
Look at the following pieces of code
let a = 1;
let b = 2;
[a,b] = [b,a]
console.log(a); // logs 2
console.log(b); // logs 1
now the same code with above, but without semicolons in the variable declarations - initializations:
let a = 1
let b = 2
[a,b] = [b,a]
console.log(a);
console.log(b);
Now it throws the error: "Uncaught ReferenceError: Cannot access 'b' before initialization".
Why does this happen?

Related

What does this declaration mean in javascript [duplicate]

This question already has answers here:
Syntax - what does square brackets around a variable declaration mean [duplicate]
(1 answer)
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Javascript. Assign array values to multiple variables? [duplicate]
(2 answers)
Closed 8 months ago.
Consider the declarations of variable "x" and "y"
const x = 1;
const [y] = [1]
What is the meaning of 2nd declaration?
Can someone suggest an article about such declaration type.
This is destructuring assignment.
You are unpacking values from arrays or object properties.
In your above code, a const variable y will be defined with value 1.

output of the javascript snippet? javascript variable declarations [duplicate]

This question already has answers here:
How JS hoisting works within functions?
(4 answers)
Why a variable defined global is undefined? [duplicate]
(3 answers)
Closed last year.
My first guess is that the following code has the output of 10 and 100. However, after running the code I get undefined and 100 and I don't understand why the function doesn't see the first declaration of x..
var x = 10;
function f() {
console.log(x);
var x = 100;
console.log(x);
}
f();

Requerid Semicolon in javascript for let variables [duplicate]

This question already has answers here:
Destructuring assignment and variable swapping
(1 answer)
Semicolon before square bracket
(2 answers)
ES6 Array destructuring weirdness
(4 answers)
Closed 1 year ago.
Please, someone cound explain this behavior with and without semicolon?
Without semicolou, it raise a ReferenceError: Cannot access 'b' before initialization
let a = 1
let b = 2
[a,b]=[b,a]
It's ok with semicolon:
let a = 1;
let b = 2;
[a,b]=[b,a]
It doesn't occurs using var.
This line, without semicolons, evaluates to let b = 2[a,b..., and since b is being declared in the same expression, it throws the error. Adding a semicolon after the 2 will remove the attempted index and resolve the error.

Why doesn't this give me the object I expected .. which is {1:2} Instead the console gives {a:2} [duplicate]

This question already has answers here:
create object using variables for property name [duplicate]
(4 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 2 years ago.
Why doesn't this give me the object I expected .. which is {1:2} Instead the console gives {a:2}
function createObject (a, b) {
const myObject = {a:b}
console.log(myObject);
}
createObject(1, 2);
Without brackets, the property name of a literal object is not evaluated as an expression:
function createObject (a, b) {
const myObject = {[a]: b}
console.log(myObject);
}
createObject(1, 2);

javaScript: Why does toString() not work directly with a number but with a number variable declared? [duplicate]

This question already has answers here:
number toString() method returns different values for number and variable [duplicate]
(4 answers)
Closed 4 years ago.
y = 15;
let x = y.toString(10);
console.log(x)
This prints '15' but
let x = 15.toString(10);
console.log(x)
gives Error: Invalid or unexpected token
Why?
It works with another dot for separating the property from number.
let x = 15..toString(10);
console.log(x);

Categories

Resources