Home > OS >  How does an "alert()" function execute in a variable
How does an "alert()" function execute in a variable

Time:07-25

var a = 'hello'-alert('hi'); // a is Not-a-Number and pops up 'hi'
var b = 'world';-alert('ok'); // b is 'world' and pops up 'ok'

Can some one explain me how does the alert() in these variable works? It seems weird as both of the alert()-s are in a variable.

CodePudding user response:

With a, substruction expect the two operators to be numbers, else the result will be NaN ( Not a Number ), and as alert is a void function it will return undefined.

With b, the ; will end the b variable declaration so it's value will be world.

CodePudding user response:

The expression on the right hand side of the assignment is evaluated before the assignment happens.

So, for a, the alert function is called and whatever its return value is is "subtracted" from the string "hello" and that result is stored in a.

For, b, the string "world" is assigned and then the statement after the semi-colon is executed.

  • Related