3

If you put on the console smth like a: "Hi" it does not show any error but it prints the value. But when you put a , it says that the variable does not exist. So, why is that? I know colons are used for defining properties inside a json object, but why is that example does not throw an error from the begging.

EDIT : I am looking for the use of colons out of a json object. Not in a switch statement either.

Flezcano
  • 1,647
  • 5
  • 22
  • 39
  • 6
    A [label](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) – MinusFour Oct 24 '17 at 17:37
  • 3
    Possible duplicate of [What does ':' (colon) do in JavaScript?](https://stackoverflow.com/questions/418799/what-does-colon-do-in-javascript) – Igor Oct 24 '17 at 17:37
  • Possible duplicate of [What does the colon (:) in JavaScript represent?](https://stackoverflow.com/questions/7147273/what-does-the-colon-in-javascript-represent) – Igor Oct 24 '17 at 17:38
  • no, its not. Those questions didn't respond it. As I stated on the original question, I was not looking for the use of the colon inside a json object – Flezcano Oct 24 '17 at 17:44

1 Answers1

11

That is a label. They can be used with continue and break when doing nested loops or nested switches, but you'll almost never run into them. Due to their rarity they may cause confusion, and so i'd generally recommend against using them unless you have a compelling reason to do so.

outer:
for (var i = 0; i < 10; i++) {
   inner:
   for (var j = 0; j < 10; j++) {
      console.log(i, j)
      if (j == 2) {
         break outer;
      }
   }
}
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98