Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is javascript's `return` really a *keyword*?

this snip will ran without any complain on both nodejs and the browser:

this.return = function ( x ) { 
    return x 
};

console.log ( this.return(1) );

I was expecting it to fail hard with a syntax error.

I meant, I can understand why this['return'] works.. but I always though return was a lexer keyword?

is javascript a context-sensitive language?

added: the point is that the lexer does not have a T_RETURN token, but it uses some T_STRING instead. Isn't?

like image 704
eridal Avatar asked Dec 05 '14 22:12

eridal


People also ask

What does the return keyword do in JS?

The return statement ends function execution and specifies a value to be returned to the function caller.

What is the meaning of return 1 in JavaScript?

-1 means the first goes before the second, 1 means it goes after, and 0 means they're equivalent. The sort function uses the comparisons in the function you pass it to sort the function. For instance, if you wanted to sort in reverse order, you could make line 3 return 1; and line 5 return -1 .

How do you call a return function in JavaScript?

Putting the parenthesis at the end of a function name, calls the function, returning the functions return value.

When should we use return in JavaScript?

There are two times you'll want to return in a function: When you literally want to return a value. When you just want the function to stop running.

Should JavaScript function always return value?

A JavaScript function can have an optional return statement i.e. it's optional to return a value. This statement should be the last statement in a function. For example, you can pass two numbers in a function and then you can expect the function to return their multiplication to your calling program.

What does empty return mean in JavaScript?

"Blank return" statements can be used to transfer the control back to the calling function (or stop executing a function for some reason - ex: validations etc). In most cases I use blank return statement is when I'm doing some kind of a validation.


1 Answers

return is a reserved keyword, but reserved keywords can be used as a property accessors without issue, it's just generally bad practice to do so.

Reserved keywords may specifically not be used as names for variables, functions, methods, or identifiers for arrays and objects, because ECMAScript specifies special behavior for them:

The source text from ECMAScript scripts gets scanned from left to right and is converted into a sequence of input elements which are tokens, control characters, line terminators, comments or white space.

ECMAScript also defines certain keywords and literals and has rules for automatic insertion of semicolons to end statements.

Reserved words actually only apply to Identifiers (vs. IdentifierNames).
As described in ES5, these are all IdentifierNames which do not exclude ReservedWords.

a.return
a["return"]
a = { return: "test" }.

However these are not

function return() {}
var return;

More on MDN

like image 99
adeneo Avatar answered Oct 20 '22 08:10

adeneo