1

Why

asdf:'qwer'

returns qwer, but

var a = asdf: 'qwer';

returns SyntaxError: Unexpected token : ?

4
  • 1
    What do you think var a = asdf: 'qwer'; is meant to do? Commented Jul 22, 2014 at 9:16
  • Friendly speaking I don't know why asdf:'qwer' doesn't return syntax error. Commented Jul 22, 2014 at 9:18
  • what you want to do?please be specific Commented Jul 22, 2014 at 9:18
  • I knew about ternary operators. This code was added accidentally before return{zxcv:'cvbn'} and I wonder why browser doesn't return syntax error Commented Jul 22, 2014 at 9:25

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.