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?
The return statement ends function execution and specifies a value to be returned to the function caller.
-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 .
Putting the parenthesis at the end of a function name, calls the function, returning the functions return value.
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.
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.
"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.
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
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