JavaScript Ambiguity between (1) and 1 [duplicate] - javascript

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()

Related

console.log weird behaviour with numbers [duplicate]

This question already has answers here:
Console.log output in javascript
(6 answers)
Closed 3 years ago.
console.log(01) results in 1
But
console.log(011) results in 9
Can someone explain how console.log works with such numbers?
It's not about console.log, a number that starts with 0 is octal notation
console.log(+"011") // if you use like this it will work
011 is an octal value and its decimal equivalent is 9. Preceding integer literal with 0 indicates octal value.

JS: why do i need brackets to call a method on number? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
What are the rules for invoking functions on number literals in JS? [duplicate]
(1 answer)
Javascript weird dot operator syntax [duplicate]
(1 answer)
Closed 5 years ago.
function func2method(f){
return function(y) {
return f(this, y);
};
}
Number.prototype.add = func2method(function(x, y){return x+y});
Why do I have to use brackets to call this method on a number?
For example, 3.add(4) won't work while (3).add(4) works perfectly fine.
Because 3.0 is not the same as (3)['0']
Literals are interpreted differently. The point . represents a decimal point on a numeric literal, but the point . on an object represents a property accessor (translated to square bracket notation above)

SyntaxError: Number.toString() in Javascript [duplicate]

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

How does JavaScript interpret indexing array with array? [duplicate]

This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 7 years ago.
[1,2,4,8][0,1,2,3]
// equals to 8 (the last element of the indexing array (3) becomes the index)
Why is this not a SyntaxError error (a bad legacy or a purposeful feature)? (A possible duplicate, however I wasn't able to find an answer here.)
Update: Why the contents of the square brackets are treated as an expression?
The first part:
[1,2,4,8]
is interpreted as an array literal. The second part:
[0,1,2,3]
is interpreted as square bracket notation to access a member of the array. The contents of the square brackets are treated as an expression, which is seen as a sequence of comma separated values:
0,1,2,3 // or (0,1,2,3) as an independent expression
That expression returns the last value, so is effectively:
[1,2,4,8][3] // 8

Javascript auto inference using .toString() [duplicate]

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."

Categories

Resources