3

Example:

let :test = "Hello";
console.log(test);

This code doesn't throw any error. Why?

3
  • 3
    The code is interpreted as let: test = "Hello"; the : is part of let which is a label here. You can tell that this is not a variable declaration because the console prints the value you assign. If it was a variable declaration it would print undefined. Also have running the code, test exists (not :test) and has the value "Hello". Same effect: foo :bar = "Hello"; Commented Mar 27, 2019 at 21:20
  • @FelixKling Thanks for the answer! One more question. Why in this case it throws an error? (without strict mode) const :test = "Hello"; console.log(test); Commented Mar 27, 2019 at 21:40
  • 1
    let is not a reserved word in non-strict mode: ecma-international.org/ecma-262/6.0/…, which means it can be used as variable name and other places. E.g. var let = 42; is valid in non-strict mode. Commented Mar 27, 2019 at 21:43

1 Answer 1

5

Because you actually have a label https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

If you were in strict mode, it would have thrown an error.

let: // <-- label
  test = "hello 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.