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

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

Related

Replace dot in a number [duplicate]

This question already has answers here:
Removing everything except numbers in a string
(7 answers)
Closed 3 years ago.
I want to replace the dot in a number in Javascript with regular expression; if country_temp is, for example, 1.234, how can I put the value 1234 in country_temp2?
I have tried the following:
const country_temp2 = country_temp.replace(/[a-zA-Z]*\**\s*\./g, "")
but it's not working.
Edit: the original string is composed by characters, asterisk, a number and a dot
Sorry, but I have written in a very fast way.
Try this:
country_temp.replace(/\./g,'')

Space character before .toFixed method [duplicate]

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

Need Guide to Use the parseInt Function [duplicate]

This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 4 years ago.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function/
I need the guide to Basic JavaScript - Use the parseInt Function (at freecodecamp)
//You need to declare the function convertToInteger taking a string parameter
//To declare a function, use the function key word and pass in your string parameter
function convertToInteger(str){
return parseInt(str);
}

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)

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