this question has answer here:
- what = +_ mean in javascript 12 answers
i came across recently, "+string" converting string number (specifically, using plus operator in front of string or string variable type coercion), wasn't able find documentation or discussion satisfied interested. impression it's lazy or have unintended side effects, want understand better. insight appreciated.
as ecmascript spec describes in section 12.5.6:
12.5.6 unary + operator
note the unary + operator converts operand number type.
so in javascript, unary +
operator (e.g., +x
) convert expression x
number. in other words, following statements equivalent:
var x = number('1234'); var y = +'1234';
this works on any variable, not strings or numbers. if object try convert not string or number, tostring()
method of object called convert object string, , converted number. example:
var obj = { tostring: function() { return '9999'; } }; var num = +obj; // = 9999
Comments
Post a Comment