Call method directly on number [duplicate] - javascript

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 4 years ago.
I recently implemented a very simple method to extend the base JavaScript Number Class and tried to call it directly on an entered number in the browser console.
123.myMethod();
But it is not working as expected, it only says: "Invalid or unexpected token"
I was unsure, if I can call Number Methods directly on entered numbers, so I tried standard methods like .toFixed():
123.toFixed(1);
But this also isn't working.
Only if I write a float, I can call Number methods:
123.0.toFixed(1);
It also works, if I put the Integer inside brackets:
(123).toFixed(1);
So my question is:
Why are Integers not implicitly casted to Number and why can't I use Number methods on them?

The . tells the JavaScript interpreter to be a decimal point, so it is expecting more numbers. In JavaScript there is only floats, no integers.

Related

convert the literal value with parentheses [duplicate]

This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 3 years ago.
I'm new to Javascript, and I saw code like this:
var myData1 = (5).toString() + String(5);
and the author says he placed the numeric value in parentheses and then called the toString method. This is because you have to allow JavaScript to convert the literal value into a number before you can call the methods that the number type defines.
I'm confused, isn't that 5 is already a number, why 5 needs be converted as (5) to be a number?
The author is partly right. This has nothing todo with turning the literal into a number, this is just about a syntactical distinction: The. can either be used to express fractional numbers (1.1) or it can be used for property access (obj.prop). Now if you'd do:
1.toString()
that would be a syntax error, as the dot is treated as a number seperator. You could do one of the following to use the property access dot instead:
1.0.toString() // as the first dot is the number seperator already, the second dot must be property access
1..toString() // same here
(1).toString() // the dot is clearly not part of the number literal

Is there any reason not to use the plus operator instead of Number() or parseInt() to return a number? [duplicate]

This question already has answers here:
parseInt vs unary plus, when to use which?
(6 answers)
What is the difference between parseInt() and Number()?
(11 answers)
Closed 4 years ago.
Basically, I'm trying to figure out what the difference is between these 3 statements? Is there any reason to use one instead of the others? Is the first one bad practice (it works but I never see it and doesn't seem to be taught anywhere)?
+'21';
Number('21');
parseInt('21');
parseInt parses the string up to the first non-digit number and returns what it found,
For example: parseInt('123abc') // returns 123;
Number tries to convert the entire string into a number if it can.
ForExample: Number('123abc') // returns NaN
Unary plus operator can also be used to convert a string into a number, but it is not very readable when it is being used with other expressions and operators
Internally, +'21' will work in the same way as Number('21') * 1
As far as I know the first two are completely equivalent, and the choice between them is a matter of taste. (Personally I prefer the unary + because it's more concise, and well understood by most JS developers.)
parseInt is different because it reads a number value from the start of the string and ignores the rest when it reaches a non-numeric character. A common use is getting the underlying number from a CSS value like "20px". Note that the other two methods would fail with a NaN in this case.

Why does a method like `toString` require two dots after a number? [duplicate]

This question already has answers here:
Why does 10..toString() work, but 10.toString() does not? [duplicate]
(3 answers)
Closed 5 years ago.
What is the logic behind 42..toString() with ..?
The double dot works and returns the string "42", whereas 42.toString() with a single dot fails.
Similarly, 42...toString() with three dots also fails.
Can anyone explain this behavior?
console.log(42..toString());
console.log(42.toString());
When you enter 42.toString() it will be parsed as 42 with decimal value "toString()" which is of course illegal. 42..toString() is simply a short version of 42.0.toString() which is fine. To get the first one to work you can simply put paranthesis around it (42).toString().
it is like 42.0.tostring() so it show's decimal point you can use (42).toString() 42 .toString() that also work there is space between 42 and dot. This is all because in javascript almost everything is object so that confusion in dot opt.
With just 42.toString(); it's trying to parse as a number with a decimal, and it fails.
and when we write 42..toString(); taken as 42.0.toString();
we can get correct output by
(42).toString();
(42.).toString();
Can refer Link for .toString() usage

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

In Javascript - Why this doesn't fail? [duplicate]

This question already has answers here:
difference between numbers using or without using variable
(3 answers)
Closed 7 years ago.
100..toString() // "100"
100 . toString() // "100"
Not able to understand why it doesn't fail? What is happening here?
Space doesn't matter but allow you to separate the floating point number and the method so 100 . toString(); returns output correctly without throwing error. And with 100.toString(), it would throw an error as JavaScript store the number as floating point number so you need to separate between floating point dot and the invocation dot so 100..toString(); returns correct result.
See related question for more info.
The first dot ensures the "100" is parsed as a float. The second accesses the member.

Categories

Resources