kotapaulta
kotapaulta

Reputation: 49

Use of colon operator ':' in javascript

Why

asdf:'qwer'

returns qwer, but

var a = asdf: 'qwer';

returns SyntaxError: Unexpected token : ?

Upvotes: 1

Views: 109

Answers (2)

Razem
Razem

Reputation: 1441

Because it can be used as a label. You can tag for example a loop so you can easily break a superior one, but it has to be a separate command:

MAIN:
while (a) {
  while (b) {
    break MAIN;
  }
}

But obviously you can add a label to anything even if it's useless.

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262949

There is no colon operator in Javascript (except as part of the ternary conditional operator ?:).

In your first snippet, asdf: is a label. In your second one, it's a syntax error, because labels are only valid before statements, not inside expressions.

Upvotes: 1

Related Questions