Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single plus operator in javascript [duplicate]

Tags:

Just saw this in underscore's source:

if (obj.length === +obj.length) {     ... } 

What does the plus do? I never saw this before.

Is it considered a good practice among developers?

like image 833
jviotti Avatar asked Jan 23 '13 01:01

jviotti


People also ask

What is the meaning of += 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.

What is plus operator in JavaScript?

Addition (+) The addition operator ( + ) produces the sum of numeric operands or string concatenation.

What is plus before variable JavaScript?

The plus(+) sign before the variables defines that the variable you are going to use is a number variable.

What is plus in typescript?

The unary plus operator ( + ) precedes its operand and converts it into a number. If it fails to convert the operand into a number, then it returns NaN.


1 Answers

The plus converts a string to a float. The code you provided is equivalent to the following:

if ( obj.length === Number(obj.length) ) {     // ... } 
like image 66
Joseph Silber Avatar answered Nov 07 '22 13:11

Joseph Silber