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.
Related
This question already has answers here:
"{} === null" throws syntax error in developer console [duplicate]
(1 answer)
Why is 0 === {} okay, but {} === 0 throws an error? [duplicate]
(1 answer)
When does JS interpret {} as an empty block instead of an empty object?
(2 answers)
{} || [] is not valid JavaScript [duplicate]
(1 answer)
Is the curly brackets object notation valid in any expression?
(5 answers)
Closed 11 months ago.
I tried this today on my browser console and saw something weird. Please help. Can someone help me understand why It happens ?
Try 1:
{}.toString(); // It shows an error - Uncaught SyntaxError: Unexpected token '.'
Try 2:
const a = {};
a.toString(); // It returns '[object Object]'
This question already has answers here:
Why does the logical or operator (||) with an empty arrow function (()=>{}) cause a SyntaxError?
(3 answers)
Closed 3 years ago.
The following code, when executed,
true && () => {}
yields
Uncaught SyntaxError: Malformed arrow function parameter list
Why ?
Edit: I know wrapping the function in parenthesis works, thanks everyone, but I'd like to understand why can't the parser figure out it's a function in the first place.
The reason is due the first part true || (a) being parsed by itself and THEN the parser is trying to parse the rest => {}, which causes the error.
It's parsing true && () as the parameter list.
Because arrow functions has special parsing rules. See official documentation at Parsing order paragraph.
This question already has answers here:
Javascript while loop return value
(3 answers)
Closed 4 years ago.
Javascript's for statement returns undefined, at least when I use it in Chromium's JS repl:
> for (i=0;i<1;i++);
: undefined
Therefore, I would expect the following statement to interpret thusly:
> a = for (i=0;i<1;i++);
: undefined
Instead I get
a = for (i=0;i<1;i++);
VM488:1 Uncaught SyntaxError: Unexpected token for
The only workaround I can think of is a = eval("for (i=0;i<1;i++);"), which does work. However, as my question states, I want to do this without using eval. Is it possible?
the for statement allowed you to iterate over a collection of items, which mean will be executing an instruction between the body of the for an example:
for (i=0;i<1;i++){
//this is the body
console.log(i);
}
the for statement doesn't return a value, what you are seeing is the dev chrome tools returning undefined.
read more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
what you might want to do is:
var myValues = []; // an array
for (i=0;i<5;i++){
values.push(i)
}
console.log(values); // [0, 1, 2, 3, 4]
This question already has answers here:
What difference does it makes to parse something as an object literal rather than as a block?
(2 answers)
Why doesn't `{}.toString.apply(array)` work?
(1 answer)
Closed 5 years ago.
When I create an object, assign it to a variable, then everything works:
var obj = {"a": 1};
obj.a; // 1
But when I remove the variable, it does not work anymore:
{"a": 1}.a // Uncaught SyntaxError: Unexpected token :
And if I enclose the object literal in parentheses, it works again:
({"a": 1}).a // 1
I think the difference is that the object literal gets interpreted as a block, and the interpreter tries to execute the "a": 1 part, which is obviously wrong. Adding parentheses forces the object creation.
Am I on the right track? What are the rules whether something is considered a block vs an object?
This question already has answers here:
JavaScript object literals syntax error
(2 answers)
Closed 7 years ago.
I´m kind of confused as to why I can assign the following object literal:
var x = { "a" : true };
While changing it to a simple statement:
{ "a" : true };
Will cause SyntaxError: Unexpected token : in Firefox and Chrome (it works as expected in Node).
JavaScript object literals syntax error
Here is a similar answer (assuming you used the REPL). Basically Node will wrap the code in parentheses, thus parsing it successfully as an object literal.
{"hello":1} to be ({"hello":1})