Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JavaScript evaluate : (colon)

A small question:

In my Browser(GoogleChrome) console if I type

abc:xyz:123

it evaluates to

123

How does JavaScript evaluate :?

like image 803
Jyoti Puri Avatar asked Dec 11 '22 05:12

Jyoti Puri


2 Answers

Both abc and xyz are treated as loop labels. Quoting from MDN,

Provides a statement with an identifier that you can refer to using a break or continue statement.

For example, you can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

Also, check Avoid using labels section,

Labels are not very commonly used in JavaScript since they make programs harder to read and understand. As much as possible, avoid using labels and, depending on the cases, prefer calling functions or throwing an error.

like image 163
thefourtheye Avatar answered Dec 23 '22 14:12

thefourtheye


I believe what is happing is abc and xyz are being treated as labels. So 2 labels are being created and then the statement of 123 is being evaluated which gives you the result of 123

like image 36
pgreen2 Avatar answered Dec 23 '22 16:12

pgreen2