Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between += and =+ in javascript

Tags:

javascript

I want to know why after running the third line of code the result of a is 5?

a = 10;
b = 5;
a =+ b;
like image 969
user1334297 Avatar asked Apr 15 '12 09:04

user1334297


People also ask

What is === and == in JavaScript?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

What is the difference between the != and !== Operators in JavaScript?

means that two variables are being checked for both their value and their value type ( 8!== 8 would return false while 8!== "8" returns true). != only checks the variable's value ( 8!=

What is the difference between {} and [] in JavaScript?

{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.

What is the difference between the operators == & ===?

KEY DIFFERENCES: = is called as assignment operator, == is called as comparison operator whereas It is also called as comparison operator. = does not return true or false, == Return true only if the two operands are equal while === returns true only if both values and data types are the same for the two variables.


1 Answers

Awkward formatting:

a =+ b;

is equivalent to:

a = +b;

And +b is just a fancy way of casting b to number, like here:

var str = "123";
var num = +str;

You probably wanted:

a += b;

being equivalent to:

a = a + b;
like image 103
Tomasz Nurkiewicz Avatar answered Sep 21 '22 22:09

Tomasz Nurkiewicz