JS documentation of numerical syntax - javascript

I have seen a syntax such as the following before:
var mynum = new Number();
var temp = (+mynum); //this line is what i am curious about
var text = temp.toPrecision(3);
Can anyone tell me what this + syntax means?
What I have found is that in some JS implementations, it is somehow necessary as it ensures that the number defined in mynum is valid.
Thanks,
jml

+ is a unary operator which is used to coerce data types into numbers. Unary meaning it only needs one operand.
new Date returns an object, applying + coerces it into a timestamp eg 1277504628812
new Number returns an object, applying + coerces it into the numeric literal 0.
See: http://bclary.com/2004/11/07/#a-11.4.6
This is the ECMAScript documentation, which is the subset of Javascript, in HTML format.

Related

Why does having a + before a string in JavaScript convert it to an Integer?

Why does the following snippet of code convert my variable of type string to type number?
let stringInteger = '42';
let convertToInteger = +stringInteger;
console.log(typeof convertToInteger)
More specifically, why does prefixing + to the variable have this effect? Note, I'm asking why not what it does.
It's the Unary Plus Operator.
Your question is answered here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_()
It's called a Unary Plus operator. It basically tries to convert non-integer variables into integers (ie +'true' and +'false' can be 1 and 0). You can read more about it on MDN and you can read more about the differences between this and other ways to parse integers in js here.

Unexpected result when using the subtraction operator with string values in Javascript [duplicate]

This question already has answers here:
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
(7 answers)
Closed 5 years ago.
Why does Javascript give an output of 0 when I use the odd operator?
What is the difference between subtraction and addition with a string?
var x = 1;
console.log(x+'1') // Outputs 11
console.log(x-'1') // Outputs 0 -- but why?
So how can I do mathematical calculations?
The + operator has one of two three meanings in javascript. The first is to add numbers, the second is to concatenate strings. When you do 1 + '1' or '1' + 1 the operator will convert one operand that is not a string to a string first, because one other operand is already evaluated to be a string. The - operator on the other hand has just one purpose, which is to subtract the right operand from the left operand. This is a math operation, and so the JS engine will try to convert both operands to numbers, if they are of any other datatype.
I'm not sure though why typecasting to strings appears to have precedence over typecasting to numbers, but it obviously does.
(It seems to me the most likely that this is a pure specification decision rather than the result of other language mechanics.)
If you want to make sure that the + operator acts as an addition operator, you can explicitly cast values to a number first. Although javascript does not technically distinguish between integers and floats, two functions exist to convert other datatypes to their number equivalents: parseInt() and parseFloat() respectively:
const x = 10;
const result = x + parseInt('1'); // 11
const y = 5;
const result2 = y + parseFloat('1.5'); // 6.5
const result3 = y + parseInt('1.5'); // 6
Edit
As jcaron states in the comment below, the + operator has a third meaning in the form of an unary + operator. If + only has a right operand, it will try to convert its value to a number almost equivalent as how parseFloat does it:
+ '1'; // returns 1
+ '1.5'; // returns 1.5
// In the context of the previous example:
const y = 5;
const result2 = y + +'1.5'; // 6.5
Dhe difference with parseFloat is that parseFloat will create a substring of the source string to the point where that substring would become an invalid numeric, whereas unary + will always take the entire string as its input:
parseFloat('1.5no-longer-valid'); // 1.5
+ '1.5no-longer-valid'; // NaN
That is because + is a concatenation operator. So javascript considers it to be a concatenation operator rather than a mathematical operator.But it is not the case with / ,* ,/ etc.
This happens because + its also used to concatenate strings. Then, JS always will find the better way to make the correct typecasts basing on types. In this case, the x+'1' operation, will be identified as string type + string type.
Otherwise, x-'1', will become int type - int type.
If you want to work with specific types, try to use type cast conversions, link here.

What does "= +" do in JavaScript?

Was reading through Douglas Crockford's code here and saw a line
var value = +node.getValue();
but I don't see anything at http://www.w3schools.com/js/js_operators.asp which corresponds to an = + or a way that + can be used as a unary operator. So what does this mean?
The - and + operators are both unary in JS and, before forcing the value's sign, must convert the value to a number.
Obviously - will convert to a number and invert the sign, but + only does the first part. Running +"100" will return the number 100.
This behavior is explicitly stated in the spec at 11.4.6, where the unary + operator is defined:
The unary + operator converts its operand to Number type.
It's just a quick way to make sure the variable is an INT, (vs. a STR or BOOL, e.g.).
Just to add on to what's been said do the following:
var a = +'4';
var b = '4';
console.log(typeof(a));//Number
console.log(typeof(b));//String

Is there a nice Javascript arithmetic-only addition operator?

By this I mean NOT the default javascript behavior of string + number = string, or string + string = string. From http://www.javascriptkit.com/jsref/arithmetic_operators.shtml, the Unary plus will convert a string to a number.
So how would I specify that I want numeric addition with two strings? This seems to work, but is somewhat ugly:
var a = "5";
var b = "2";
var c = +a + +b;
Does there exist a (numeric only +) operator, that always returns a number? Can one be defined like in other languages? For examples, perhaps '%%' or '+^' or '+++'? Or is this just not possible?
No. If you want to perform numeric addition than you have to ensure that both sides of the expression are Numbers before you start.

what the meaning of plus operator in this JQUERY scripts

I want to ask mean of plus operator in this script +i+ in the follow:
i=0;
// next line in scripts write this code :
$('.container[data-id='+i+']').hide(); // +i+ what the meaning of it
Need Help thanks a TON
+ is used to concatenate strings
In this case, it is used to concatenate the value of ibetween .container[data-id= and ]
Assuming that i is storing some value eg 0, then this will be evaluated to
$('.container[data-id=0]').hide();
This will concatenate the value in the variable i with the remaining string.
See Concatenating strings
+ is used for string concantination
var test= "hello" + "world";// gives helloworld
it is used for adding values also
var c= a+ b// if a=10 and b=20 it gives 30
The + operator in JavaScript is used for both Arithmetic and String operations.
If both operands are of Number type, the result is the sum. If either of the operands is not a number, then both will be converted to String and concatenated. It should be used with care.

Categories