Home > Software engineering >  What is these "letter:" behind the code and what is used for?
What is these "letter:" behind the code and what is used for?

Time:01-28

f: function teste() {
  return 122
}


a: function teste2() {

}
b: console.log('teste')

I'm not sure why this works. I'm running this code in NodeJS 16 and I want to know what is the letter before any code. And how do I use it effectively?

CodePudding user response:

These are labels for statements. In your example b is the label for the console.log('teste') statement.

Labels aren't often used, they are similar to using GOTO. But when they are used, they are used with break or continue so you can be explicit to where you're breaking or continuing the code run from.

As pointed out by @ruan-mendes, labels are invalid on statements in strict mode.

You can read more about them here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

  • Related