Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

+ operator before expression in javascript: what does it do?

I was perusing the underscore.js library and I found something I haven't come across before:

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

What is that + operator doing there? For context, here is a direct link to that part of the file.

like image 436
JoeM05 Avatar asked Nov 30 '11 17:11

JoeM05


People also ask

What is operator and expression in JavaScript?

An assignment operator assigns a value to its left operand based on the value of its right operand. The simple assignment operator is equal ( = ), which assigns the value of its right operand to its left operand. That is, x = f() is an assignment expression that assigns the value of f() to x .

What are the 4 types of JavaScript operators?

JavaScript includes operators that perform some operation on single or multiple operands (data value) and produce a result. JavaScript includes various categories of operators: Arithmetic operators, Comparison operators, Logical operators, Assignment operators, Conditional operators.

What is ___ operator in JavaScript?

JavaScript +_ operator: It is a combination of the variable with symbol underscore( _ ) and unary plus ( + ) operator, + operator converts the type of _ variable to Number type for further operation. Example: The variable “_” with string type is stored in variable “r” with “number” type. Input: var _ = "1"; var r = +_;

What is an expression operator?

Expressions perform specific actions, based on an operator, with one or two operands. An operand can be a constant, a variable or a function result. Operators are arithmetic, logical, and relational.


3 Answers

The unary + operator can be used to convert a value to a number in JavaScript. Underscore appears to be testing that the .length property is a number, otherwise it won't be equal to itself-converted-to-a-number.

like image 188
Jeremy Avatar answered Oct 24 '22 01:10

Jeremy


According to MDN:

The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. For example, y = +x takes the value of x and assigns that to y; that is, if x were 3, y would get the value 3 and x would retain the value 3; but if x were the string "3", y would also get the value 3. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.

like image 28
FishBasketGordo Avatar answered Oct 24 '22 02:10

FishBasketGordo


It's a way of ensuring that obj.length is a number rather than a potential string. The reason for this is that the === will fail if the length (for whatever reason) is a string variable, e.g. "3".

like image 32
Ben Clayton Avatar answered Oct 24 '22 01:10

Ben Clayton