What is the use of colon in javascript

The following block of code completed successfully. I was wondering what would use this labeling besides use for loops?

<script> js: { alert("x"); } </script> 
+6
source share
5 answers

This is how you mark tags, a very bad practice that makes it possible to implement the old "goto", which is a simple transition to the code in sequential execution

+3
source

: has several uses in javascript that I know anyway.

  • ternary operator - used to calculate the if on a single line:

     var x = "yes" == "yes" ? true : false; 

    The above line of code is functionally equivalent:

     if("yes" == "yes"){ var x = true; } else{ var x = false; } 
  • Mark start of code block - Move to code block

     begin: for(int i = 0; i < 10; i++){ break begin; } 
  • Object Literals - Thanks @ I'm for the reminder

     var someObject= { item: 'some value', anotherItem: 2 // Can put any type of variable here }; 

    This type of notation is commonly observed when using JSON.

+5
source

This is the designation. You can use marked statements in the form of break and continue statements:

 outer: for (var i = 0; i < 1000; ++i) { for (var j = 0; j < 1000; j++) { if (somethingBad()) break outer; } } 

It is (rarely) useful in order to be able to go from the inner nested loop to the outer level of iteration. I don’t think I have ever used it in many thousands of lines of code. In the sample code posted in the original question, the shortcut does not have an obvious purpose.

+3
source

It is called Shortcuts [MDN] in javascript

 myPrettyLabel: { alert('testMe'); } 

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

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

Same as goto in shared programs


Note,

Avoid using labels because labels are not used very often in JavaScript, as they make programs more difficult to read and understand. Avoid using labels whenever possible and, depending on the case, prefer to call functions or throw an error.


Link: SO - What is a colon (:) in JavaScript?

+3
source

A colon is used to add a label. As explained by the MDN documentation for the label :

Provides an operator with an identifier that you can reference using the break or continue statement.

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

MDN also contains sample code:

 var i, j; loop1: for (i = 0; i < 3; i++) { //The first for statement is labeled "loop1" loop2: for (j = 0; j < 3; j++) { //The second for statement is labeled "loop2" if (i == 1 && j == 1) { continue loop1; } else { console.log("i = " + i + ", j = " + j); } } } // Output is: // "i = 0, j = 0" // "i = 0, j = 1" // "i = 0, j = 2" // "i = 1, j = 0" // "i = 2, j = 0" // "i = 2, j = 1" // "i = 2, j = 2" // Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2" 

In addition, as indicated in the article, such logic tends to make your code more understandable, so you better not restructure the code to use other types of flow control (functions, etc.) to avoid the need to use labels.


Note that this syntax can be confusing, as it is also similar to object syntax, for example:

 { js: {1}, ... } 
-2
source

Source: https://habr.com/ru/post/943624/


All Articles