This question already has answers here:
Calling member function of number literal
(3 answers)
1.toString() SyntaxError in Javascript
(4 answers)
Closed 8 years ago.
Why doing this
123.toString()
gives an error...
but this
(123).toString()
Works OK
if possible and "at low level" answer (JIT/Interpreter parsing tokens decisions)
The JavaScript interpreter considers the . to be a decimal point, and therefore does not expect an alphabet character to follow it, hence the "unexpected token" error. Putting 123 in parentheses tells the interpreter, "run toString() on this object."
Related
This question already has answers here:
Call methods on native Javascript types without wrapping with ()
(5 answers)
Strange syntax of Number methods in JavaScript
(5 answers)
Whitespace before dot (method call) allowed in Javascript?
(3 answers)
Closed 3 years ago.
I noticed that if you put a space character before .toFixed method, it will work without errors. I understand why it's work with a parenthesis (2).toFixed(2), but why does it work with space?
console.log(2 .toFixed(2))
// 2.00
console.log(2.toFixed(2))
// Uncaught SyntaxError: Invalid or unexpected token
Please leave links to any information about this. Thanks.
It is just the usual whitespace before the (dot) operator.
Maybe you have a look here, too: dot notation
console.log(2 .toFixed(2))
This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 4 years ago.
I know, weird question. I accidentally pasted an expression in the the Chrome developer tools console but copied it without the function call so instead of:
dayDiff(date1, date2)
I just ran
(date1, date2)
with the variables in parenthesis separated by a comma, which simply returns the value of date2. I'm trying to understand what the engine is interpreting this expression as. I would've expected this to be a syntax error.
It is comma operator. It simply evaluates both arguments and returns the RHS value.
This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Calling member function of number literal
(3 answers)
Closed 6 years ago.
What is difference between (1) and 1.
1.toString() //Will throw error
1.toFixed(1) //Will throw error
(1).toString() // output "1"
(1).toFixed(1) // output 1.0
The trailing period on 1. is part of the number -- the compiler reads it as a decimal point, not as the dot operator. This makes an identifier immediately following the number unexpected.
Consider, for comparison: 1.0toString()
This question already has answers here:
Explain +var and -var unary operator in javascript
(7 answers)
Closed 6 years ago.
I just got a bug that took some of my time to spot my searching filters weren't working because of the following code :
queryObject.search='valid==true';+searchQuery;
The good syntax is to mive the ';' in the string :
queryObject.search='valid==true;'+searchQuery;
The reasn why i didn't spot that is because the earlier line of code didn't triggered any javascript console error. So it seems it's a valid syntax.
So here is my question, how can this be a valid syntax ?
+something is an expression using the plus unary operator.
Its general purpose is to convert a value, for example a string, to a number.
+ is unary operator, which tries to get numeric value from variable.
There is a thread about it.
This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 6 years ago.
Why do I get an error on this, in Javascript:
10000000.toString();
You can see an example in here:
http://jsbin.com/kagijayecu/1/edit?js,console
It's because the JS parser is expecting more digits after the "." rather than a method name, e.g. 1000000.0, and in fact 1000000.0.toString() will work as expected.
wrap it inside () like this (10000000).toString()
When JS parse meets dot after digit it expects floating-point literal, e.g. 1000000.0