Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript "Equal Sequence" meaning

Sometimes in the internet I see a syntax that is strange to me. Something like:

console.log = console.error = console.info = console.debug = console.warn = console.trace = function() {}

How does this "equal" sequence work?

Thanks.

like image 490
Fabio Sampaio Avatar asked Nov 25 '16 21:11

Fabio Sampaio


People also ask

What does it mean += in JavaScript?

The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator.

Does JavaScript have -=?

The subtraction assignment operator ( -= ) subtracts the value of the right operand from a variable and assigns the result to the variable.

Is it += or =+ in JavaScript?

the correct syntax is a+=b; a=+b; is not correct. it is simply assigning b value to a.

What does $$ mean in JavaScript?

$ and $$ are valid variable names in JavaScript, they have no special meaning. Usually they set their value to library instances, in your example if you check the closure call, at the end of the file you'll see that $ is jQuery in this case if it is defined and $$ is cytoscape.


2 Answers

An assignment operator assigns a value to its left operand based on the value of its right operand.

Consider:

a = b = c = d = 5;

The expression is resolved right to left so:

d = 5 and c = d (which is 5), b = c (5) and so on.

In your example those console methods are all being (re)defined as an empty function.


See: MDN: Assignment Operators for more info.

like image 57
Moob Avatar answered Oct 02 '22 19:10

Moob


With assignments, the operations are resolved from right to left. So the right most value will be populated into all the preceding variables.

like image 40
Taplar Avatar answered Oct 02 '22 17:10

Taplar