Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript colon (:) operator inside function

Tags:

javascript

I apologize if this question is stupid or duplicated, please point me to the right direction if so.

I've tested this code:

function b()
{
    a: 22;
    return a;
}

Code returns no error. Which leads me into thinking a: 20 inside function works. Though when call the function:

b();

..I receive "ReferenceError: a is not defined"

What does a: 22 inside function actually do? this.a inside function returns nothing so I don't think it has something to do with function as object (or it does?..)

like image 683
user8555937 Avatar asked Dec 06 '25 07:12

user8555937


1 Answers

The a: 22 in your code is a label – it labels the statement 22 with the identifier a.

From MDN:

The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to.

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

like image 179
Daniel Diekmeier Avatar answered Dec 08 '25 19:12

Daniel Diekmeier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!