I'm learning programming with JavaScript, and I just don't know what's happening here:
let myVar = ('Hello','World!');
console.log(myVar);
The output is:
World!
I'm learning programming with JavaScript, and I just don't know what's happening here:
let myVar = ('Hello','World!');
console.log(myVar);
The output is:
World!
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
This means that the expression 'Hello','World!'
evaluates to 'World!'
You're setting the variable to the string "Hello", then setting it to "World". The same thing happens if you set a list of variables inside a parenthetical.
const myVar = (1,2,3,4,5,6);
console.log(myVar);
//6
Where as I think what you were trying to do was use a grouping operator like +
.
const myVar = ("Hello, " + "World")
console.log(myVar)
//"Hello, World"