Sergio Romero
Sergio Romero

Reputation: 6607

Unknown JavaScript syntax before for...in

I am reading a book about single-page applications and at some point there is this for...in loop:

KEYVAL:
for(key_name in arg_map){
    if(arg_map.hasOwnProperty(key_name)){
        if(key_name.indexOf('_') === 0) continue KEYVAL;

        anchor_map_revise[key_name] = arg_map[key_name];

        key_name_dep = '_' + key_name;
        if(arg_map[key_name_dep]){
            anchor_map_revise[key_name_dep] = arg_map[key_name_dep];
        }
        else{
            delete anchor_map_revise[key_name_dep];
            delete anchor_map_revise['_s' + key_name_dep];
        }
    }
}

What really caught my eye was the KEYVAL word right before the loop. Is it a variable? What does it represent? What is it for? What does this syntax mean? The word only appears in the two places in the included code and never again in the whole example.

I've been searching a lot trying to figure this out, but so far I have not been able to find any information. Could someone please help me out?

Thank you.

Upvotes: 2

Views: 305

Answers (3)

Hogan
Hogan

Reputation: 70531

It is for break and continue.

Check the MDN

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

Upvotes: 1

Sachin
Sachin

Reputation: 40970

KEYVAL: is a label here which is used in your loop for iteration the operations. Look at the documentation

It is similar to GOTO statements.

What it does in your code is when this condition become true

if(key_name.indexOf('_') === 0) continue KEYVAL;

its go to the label KEYVAL: and run the loop again without executing the code below this line.

Upvotes: 3

user1693593
user1693593

Reputation:

It is a label, sort of a line-number but not locked to a line position.

Continue jumps to this label like a GOTO.

When this criteria is furfilled:

if(key_name.indexOf('_') === 0) continue KEYVAL;

JavaScript continues from that label above.

Upvotes: 2

Related Questions