Space character before .toFixed method [duplicate] - javascript

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

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

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

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

Comma separated number/function in parenthesis in JavaScript? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 6 years ago.
I read a line from doT.js:
var global = (function(){ return this || (0||eval)('this'); }());
After it was minified:
l=function(){return this||(0,eval)("this")}();
So what is the (0,eval), I mean what does the comma do?
I played in Chrome's console, (0,1), (2,1), (2,{}), 2,1, etc, it always returns the last one.
The comma operator evaluates both and always returns the last. Much like you said.
You can read up on the comma operator: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/
Even though I have no idea the purpose of (0||eval)... (0,eval) is the equivalent and one less character.

Categories

Resources