Single plus operator in javascript
The plus converts a string to a float. The code you provided is equivalent to the following:
if ( obj.length === Number(obj.length) ) {
// ...
}
Plus operator with javascript objects
The difference is because, even though the two pluses look identical, they are in fact different operators.
In the former case, you have a binary +
, meaning it has two operands (one on either side). As with many other languages, the binary +
in JS has many functions depending on the types of the operands. Here, it does string concatenation, hence the result you get (the string representations of the two operands, joined together).
In the latter case, you have a unary +
, meaning it only has one operand. In the JS specification, the unary plus' job is to coerce its operand into a number. Since that is not possible with an empty object, you get NaN
instead.
What is the purpose of a plus symbol before a variable?
The +
operator returns the numeric representation of the object. So in your particular case, it would appear to be predicating the if on whether or not d
is a non-zero number.
Reference here. And, as pointed out in comments, here.
JavaScript plus sign in front of function expression
It forces the parser to treat the part following the +
as an expression. This is usually used for functions that are invoked immediately, e.g.:
+function() { console.log("Foo!"); }();
Without the +
there, if the parser is in a state where it's expecting a statement (which can be an expression or several non-expression statements), the word function
looks like the beginning of a function declaration rather than a function expression and so the ()
following it (the ones at the end of the line above) would be a syntax error (as would the absense of a name, in that example). With the +
, it makes it a function expression, which means the name is optional and which results in a reference to the function, which can be invoked, so the parentheses are valid.+
is just one of the options. It can also be -
, !
, ~
, or just about any other unary operator. Alternately, you can use parentheses (this is more common, but neither more nor less correct syntactically):
(function() { console.log("Foo!"); })();
// or
(function() { console.log("Foo!"); }());
What does = +_ mean in JavaScript
r = +_;
+
tries to cast whatever_
is to a number._
is only a variable name (not an operator), it could bea
,foo
etc.
+"1"
cast "1" to pure number 1.var _ = "1";
var r = +_;
r
is now 1
, not "1"
.Moreover, according to the MDN page on Arithmetic Operators:
It is also noted thatThe unary plus operator precedes its operand and evaluates to its
operand but attempts to converts it into a number, if it isn't
already. [...] It can convert string representations of integers and
floats, as well as the non-string valuestrue
,false
, andnull
.
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 toNaN
.
unary plus is the fastest and preferred way of converting something into a number
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
String concatenation is done with +
so Javascript will convert the first numeric 1 to a string and concatenate "1" and "1" making "11".
You cannot perform subtraction on strings, so Javascript converts the second "1" to a number and subtracts 1 from 1, resulting in zero.
what's the use of having plus sign infront of an expression
It converts the string number to actual number in the expression.
console.log(typeof +"1"); // number
console.log("1" + "1"); // 11
console.log(+"1" + +"1"); // 2
console.log("1.3" + "1.546"); // 1.31.546
console.log(+"1.3" + +"1.546"); // 2.846
Quoting from the ECMA 5.1 Standard Specifications for + operator,Internally, a JavaScript string will be converted to a number based on these rules specified in the ECMA 5.1 standards.The unary + operator converts its operand to Number type.
Edit: As per the Number
specifications, it also internally uses the same ToNumber
to convert its parameter to a number. So, technically Number(<number string>)
is the same as +<number string>
.
+ operator before expression in javascript: what does it do?
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.
Can someone explain to me what the plus sign is before the variables?
+a // + is used to be sure that a is a number before adition
+ // is plus sign for adition!
+b // + here is used to be sur that b is number before adition
if we were sure that a and b are numbers (there where no need for that)//you can replace + sign with
parseInt(a) + parseInt(b)
//or
parseFloat(a) + parseFloat(b)
as an examlpe imagine what hepens if you forget the + sign"1" + 1 = "11"
//but
+"1" + 1 = 2
What does the plus sign do in '+new Date'
That's the +
unary operator. It's equivalent to:
function(){ return Number(new Date); }
See http://xkr.us/articles/javascript/unary-add and MDN.
Related Topics
JavaScript Listener, "Keypress" Doesn't Detect Backspace
How to Send Images to Node Js with Axios
Create Copy of Multi-Dimensional Array, Not Reference - JavaScript
Regular Expression for Url Validation (In JavaScript)
JavaScript Roundoff Number to Nearest 0.5
Single Quote Escape in JavaScript Function Parameters
Razor MVC Populating JavaScript Array with Model Array
JavaScript - Generating All Combinations of Elements in a Single Array (In Pairs)
Remove Zero-Width Space Characters from a JavaScript String
JavaScript - Telling Setinterval to Only Fire X Amount of Times
Splicing a JavaScript Array from Within the Callback Passed to Foreach
What Do Double Brackets Mean in JavaScript and How to Access Them
Backbone: Why Assign '$('#Footer')' to 'El'
What's Wrong with Var X = New Array();
How to Detect When a Tab Is Focused or Not in Chrome with JavaScript