Asked  7 Months ago    Answers:  5   Viewed   18 times

If I try to write


there is a syntax error. Using double dots, putting in a space, putting the three in parentheses or using bracket notation allows it to work properly.

3 .toFixed(5)

Why doesn't the single dot notation work and which one of these alternatives should I use instead?



The period is part of the number, so the code will be interpreted the same as:


This will naturally give a syntax error, as you can't immediately follow the number with an identifier.

Any method that keeps the period from being interpreted as part of the number would work. I think that the clearest way is to put parentheses around the number:

Tuesday, June 1, 2021
answered 7 Months ago

at the start of a statement signals a ‘statement block’ (see ECMA-262-3 section 12.1), which contains a list of statements.


immediately ends the statement block with no statements in it. That's fine. But now the parser is looking for the next statement:


Huh? That's not a statement; syntax error.

What are statement blocks for? Well, you are writing a statement block every time you say:

if (something) {

JavaScript defines these flow-control statements as:

if "(" <expression> ")" <statement> [else <statement>]

ie. in the single statement form with no braces. It then allows you to use a statement-block anywhere you can use a single statement, which means you can have if-braces-many-statements. But it also means you can have a statement-block on its own with no associated flow-control statement.

This serves absolutely no practical purpose! You might be tempted to think it gave you information-hiding, but no:

var a= 1;
    var a= 2;

...results in 2, because statement blocks don't in themselves create a new scope.

JavaScript defines flow control and statement blocks in this manner because C (and other languages derived from it) did. Those languages didn't make {} serve double-duty as an Object literal expression though, so they didn't have the ambiguity that makes this another JS misfeature.

Even this wannabe-literal:

   a: 1

is a valid statement block, because ‘:’ is used to denote a label in a statement. (and 1 is a useless expression-statement, with the semicolon omitted.) Labels are another feature inherited from C that are rarely used in JavaScript. They're not totally pointless like the blocks, but they're seldom needed and often considered in poor taste.

(A literal with two properties will cause a syntax error, as object literals use comma separators, but labelled statements must be separated by semicolons.)

This is not the only place where JavaScript's loose syntax can trip you up by making some other statement of something you think is an expression.

Thursday, July 29, 2021
answered 5 Months ago

return only makes sense inside a function. There is no function in your code.

Also, your code is worthy if the Department of Redundancy Department. Assuming you move it to a proper function, this would be better:

return confirm(".json_encode($message).");

EDIT much much later: Changed code to use json_encode to ensure the message contents don't break just because of an apostrophe in the message.

Monday, August 2, 2021
answered 5 Months ago

Property names are always strings or symbols.

If you pass something which isn't a string or symbol, it gets converted to a string.

The default toString() method on an array is roughly:

String.prototype.toString = function () { return this.join(","); }

So ['a'] gets converted to 'a'.

As a side note this only works with an array of length 1.

It works fine with arrays that are longer. You just need a matching value:

const o = {
    "a,b": "Hello"
const a = ["a", "b"];
console.log("" + a);

And since any object can be converted to a string, and you can customise the toString method, you can do really weird things:

const data = {
  "42": "Hello"

class Weird {
    constructor(x) {
        this.x = x;
    toString() {
        return this.x + 40;

const w = new Weird(2);

(Note that doing really weird things is usually a stupid idea that makes it hard to debug your own code two weeks later).

Friday, August 6, 2021
Igor Tupitsyn
answered 4 Months ago

As others have said that's because of octal numbers. But I strongly suggest you to change your function to:

>>> from functools import partial
>>> force_decimal = partial(int, base=10)
>>> force_decimal("01")
>>> force_decimal("0102301")

This way you will explicitly force the conversion to base 10. And int wont be inferring it for you.

Thursday, August 19, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :