9

I have just noticed that I can do the following in javascript...

a:b:c:d = "happy days";
a:b:c = function(text){alert(text);};

I cannot however do...

var a:b:c:d = "happy days"; 
// or
var myObj = {prop:a:b:c:d};

I was not expecting this syntax to work under any circumstances. Does anyone have any idea what is going on when I successfully use the 'a:b=x' notation?

5
  • That's peculiar, that is. You can even read the value back again, which I really didn't expect. Commented Jan 9, 2012 at 1:01
  • 1
    a:b:c:d = "happy days"; assigns 'happy days' to d. not to a:b:c:d.. Commented Jan 9, 2012 at 1:02
  • After you initialize a:b:c:d, type a:bbbb:cccccc:garble:d Commented Jan 9, 2012 at 1:02
  • 2
    Side note: this is perfectly legal: window["a:b:c:d"] = "happy days"; Commented Jan 9, 2012 at 1:07
  • @lwburk - Thanks for this. Your comment was the nudge I needed to get find the solution to the problem which raised this question. See below. Commented Jan 13, 2012 at 0:57

2 Answers 2

17

A colon in ECMAscript is there for three reasons

  • separating object keys from its values
  • inline conditional statements
  • labeling

you discoverd the latter. What you are basically doing is creating a label called a, then b, then c and finally you are assigning a value to a global variable d. So after

a:b:c:d = "happy days";

console.log(a); // reference error
console.log(d); // "happy days";

Most common usage for this is within a switch statement where we do it like

switch( foo ) {
    case 0: break;
    case 1: break;
    // etc
}

But you can also directly "target" a label with the continue statement. That comes very close to goto in many other languages and looks like

foobar:
for(var i = 0; i < 10; i++) {
    for(var j = 0; j < 10; j++) {
        if( j === 2 )
            continue foobar;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I agree that these are labels, but I think "case x:" is an alternative use of the colon, as labels seem to serve strictly as reference points for break and continue statements, not switch statements. Also, if you want to be complete, the colon is also used in inline switch statements (a?b:c;).
@skier88: yes you're right, forgot about inline conditionals. For the switch/case, I always thought that a case is pretty much treated as a label, but I'll have a look for that.
5

Quoting the ECMAScript standard: “A Statement may be prefixed by a label. Labelled statements are only used in conjunction with labelled break and continue statements.” A label consists of an identifier and a colon. So a:b:c:d = "happy days"; is just an assignment statement d = "happy days"; prefixed by three labels, which have no effect as such.

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.