Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of the following colon in javascript

Tags:

javascript

Following code block executed successfully. I was wondering what would be the use of this labeling other than using for loops?

<script>

js:
{
   alert("x");  
}

</script>
like image 813
Kuzey Avatar asked Apr 25 '13 14:04

Kuzey


People also ask

What is :: in JS?

Show activity on this post. :: is obviously used here to allow either one or two arguments to be passed to the function.

What is double colon in JavaScript?

The Bind Operator :: # The Bind operator consists of an introduction of a new operator :: (double colon), which acts as syntax sugar for the previous two use cases. It comes in two formats: binary and unary.

What Does a colon do in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.

What does double colon mean in TypeScript?

The :: is a proposed binding operator that desugars into a bound function: ::foo. bar // becomes foo. bar. bind(foo) This is useful in React (and any other event handlers) because it means this will have the expected value (instance of the class) when the event handler is later invoked.


3 Answers

The : has a few uses in javascript, that I know of anyway.

  1. ternary operator - used to evaluate an if statement in a single line:

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

    The above line of code is functionally equivalent to:

    if("yes" == "yes"){
        var x = true;
    }
    else{
        var x = false;
    }
    
  2. Mark the beginning of a code block - Move to a block of code

    begin:
    for(int i = 0; i < 10; i++){
        break begin;
    }
    
  3. Object Literals - Thanks @Ian for the reminder

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

    This type of notation is commonly seen when using JSON

like image 175
What have you tried Avatar answered Oct 05 '22 23:10

What have you tried


It is called Labels [MDN] in javascript

myPrettyLabel:
{
   alert('testMe');
}

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.

Same as goto in general programs


Please note

Avoid using labels as 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.


Reference: SO - What does the colon (:) in JavaScript represent?

like image 21
Harsh Baid Avatar answered Oct 06 '22 00:10

Harsh Baid


It's a labeled statement. You can use labeled statements with a form of the break and continue statements:

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

It's (rarely) useful to be able to get out of an inner nested loop to an outer iteration level. I don't think I've ever used it across many thousands of lines of code. In the example code posted in the original question, the label has no apparent purpose.

like image 20
Pointy Avatar answered Oct 05 '22 23:10

Pointy