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>
Show activity on this post. :: is obviously used here to allow either one or two arguments to be passed to the function.
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.
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.
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.
The :
has a few uses in javascript, that I know of anyway.
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;
}
Mark the beginning of a code block - Move to a block of code
begin:
for(int i = 0; i < 10; i++){
break begin;
}
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
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
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With