Requerid Semicolon in javascript for let variables [duplicate] - javascript

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.

Related

you might need to wrap the the whole assignment in parentheses [duplicate]

This question already has answers here:
What is this javascript syntax where you have () around the whole variable expression?
(2 answers)
How to destructure an object to an already defined variable? [duplicate]
(1 answer)
Closed 6 months ago.
when run in console:
let b;
{b} = {b:1} // run without error
{b} = {b:1} // empty line before this code, and run with error VM243:2 Uncaught SyntaxError: Unexpected token '='
({b} = {b:1}) // run without error
when run in https://www.typescriptlang.org/play, error message like below:
Declaration or statement expected. This '=' follows a block of statements, so if you intended to write a destructuring assignment, you might need to wrap the the whole assignment in parentheses.

Non optional semicolons in JavaScript? [duplicate]

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?

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.

Evaluating expressions and parentheses in JavaScript [duplicate]

This question already has answers here:
How is division before multiplication in Javascript?
(2 answers)
Closed 4 years ago.
I'm having trouble understanding how JS is processing my expressions..
This evaluates incorrectly and drops the denominator:
var a = document.getElementById("Acceleration").value;
var u = document.getElementById("Initial_Velocity").value;
var v = document.getElementById("Final_Velocity").value;
document.getElementById("Distance").value = (v*v-u*u)/2*a;
This evaluates properly:
document.getElementById("Distance").value = (v*v-u*u)/(2*a);
I'm using Dreamweaver. Thanks.
Well the formulas are different. You left out the parentheses (2*a) in the first code sample.

Why is a semicolon needed here? [duplicate]

This question already has an answer here:
Destructuring assignment in while loop in ES6 function doesn't propogate out of loop?
(1 answer)
Closed 6 years ago.
I am simply using es6 to reassign variables a & b. Why do I get an error if I leave the semicolon off of the a and b declaration and assignment statements? Does the parser try to pull a property out of 2 if the semicolon is left off? let b = 2[a, b]...?
Works:
let a = 1;
let b = 2;
[a, b] = [b, a]
Error:
let a = 1
let b = 2
[a, b] = [b, a]
I am looking for the actual reason this fails. Thanks!
Does the parser try to pull a property out of 2 if the semicolon is left off? let b = 2[a, b]...?
Yes. You cannot safely start a statement with a [ if you're not explicitly ending statements with semicolons.
This has been written about in many places, and it's one of the many things that standardJS (or other linters) will warn you about: http://standardjs.com/rules.html#semicolons

Categories

Resources