Label (Definition)

An identifier marking a section of code.

Availability:

ECMAScript edition - 3

In JavaScript version 1.2, the case and default labels were added, which introduced as a by-product the fact that any fragment of code can be labelled with an identifier.

The identifier can be any legal JavaScript identifier that does not match a reserved keyword.

The namespace that labels exist in, is separate to that of variables and function names. This means you can use the same identifier names over again, although it's probably good practice not to.

Adding a label in front of an iterator allows you to associate a label name with a break or continue statement. This means that you can break or continue nested iterators from deep inside them. This can greatly simplify the logic of a looping system.

If the goto keyword is ever implemented, it would depend on this labelling mechanism being extended to provide a useful destination.

Example code:

   // An example break to a labelled line

   outerLoop:

   for (var i=0; i <= max; i++)

   {

      for (var j=0; j <= max2; j++)

      {

         if (i== someNum && j ==someNum2)

         {

            break outerLoop;

         }

      }

   }

See also:break, Code block delimiter {}, continue, do ... while( ... ), for( ... ) ..., for( ... in ... ) ..., goto, Reserved Word, while( ... ) ...

Cross-references:

ECMA 262 edition 3 - section - 12.12