Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript a=b=c statements

Tags:

javascript

I searched through the internets but could not find a relevant search criteria so I thought this would be the best place to ask.

I have a JS statement saying

document.location.hash = this.slug = this.sliceHashFromHref(href) 

How does this work??

like image 621
amit Avatar asked Sep 22 '11 07:09

amit


People also ask

What are the 5 JavaScript statements?

JavaScript statements are composed of: Values, Operators, Expressions, Keywords, and Comments.

What are the three types of statements in JavaScript?

The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. The test statement which will test if the given condition is true or not.

What is meant by ${} in JavaScript?

In Javascript the ${} is used to insert a variable to a string.

What is C in JavaScript?

JavaScript hides this power. C is commonly used for embedded computers and applications that require high performance such as operating systems. JavaScript was first embedded only in web pages, but it is finding a new role in server applications developed through Node. js.


2 Answers

How does this work??

a = b can be seen as both a statement and an expression.

The result of the expression is b.

In other words,

a = b = c; 

which can be written as

a = (b = c); 

is equivalent to

b = c; a = b; 

Thus your code is equivalent to:

this.slug = this.sliceHashFromHref(href); document.location.hash = this.slug; 
like image 123
aioobe Avatar answered Sep 21 '22 14:09

aioobe


Be aware of the variables scope!!

var A = B = C = 3; //A is local variable while B & C are global variables; var A = 3 , B = 3, C = 3;// A B C are local variables; 
like image 27
Ma Jerez Avatar answered Sep 22 '22 14:09

Ma Jerez