This question already has answers here:
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
Closed 6 years ago.
I have written 2 javascript function but they are not working as same.
console.log(func2()); is undefined. Can anyone tell me why and how to solve this?
function func1()
{
return {
bar: "hello"
};
}
function func2()
{
return
{
bar: "hello"
};
}
console.log(func1());
console.log(func2());
It's because of automatic semicolon insertion. Never put a newline after return and prior to what you want to return, it's treated as though it terminates the return statement (e.g., a ; is inserted after return), and your function ends up effectively returning undefined.
I know this one. It's semicolon insertion. func2 is translated to
function func2()
{
return;
{
bar: "hello"
};
}
And return undefine.
Related
This question already has answers here:
Javascript function fails to return object when there is a line-break between the return statement and the object?
(2 answers)
Closed 2 years ago.
** I have created two functions with different name & same properties and when I was checking surprisingly it's giving me different output. I don't understand what was is the problem exactly. I was expecting same result for both functions. See the below code**
function foo1()
{
return {
bar: "hello"
};
}
function foo2()
{
return
{
bar: "hello"
};
}
console.log("foo1 returns:");
console.log(foo1());
console.log("foo2 returns:");
console.log(foo2());
Below is the Output:
foo1 returns:
Object {bar: "hello"}
foo2 returns:
undefined
It's because you have a new line after your return statement in the second function.
So instead of returning the object with bar, it just returns without a specified value, which returns undefined.
This question already has answers here:
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
Javascript function fails to return object when there is a line-break between the return statement and the object?
(2 answers)
Why doesn't a Javascript return statement work when the return value is on a new line?
(3 answers)
Closed 3 years ago.
I’m still trying to get to grips with function definitions: am I correct in saying that this returns a type of undefined?
function foo() {
return
{
car: 'Audi'
};
}
It returns undefined, because of Automatic Semicolon Insertion. This is one of the major ASI hazards. Having a line break after return makes it get treated as return;. So your code ends up being:
function foo() {
return; // <=== Function returns here
{ // \
car: 'Audi' // > This is a freestanding block with a labelled statement which is the expression 'Audi'
}; // /
}
Remove the line break and it returns an object:
function foo() {
return {
car: 'Audi'
};
}
This is one reason that putting the opening { on things is standard practice in JavaScript, even more so than other C-like languages.
Javascript assumes return to be return;, if you want to return any value it should be in the same line as return.
If you are getting undefined when you execute this function foo, that's because the function is returning at return itself, to get the object as return value, modify your function to be
function foo() {
return {
car: 'Audi'
};
}
This question already has answers here:
Is returning early from a function more elegant than an if statement?
(14 answers)
Is it good style (or more efficient) to "return early" at the top of a function?
(2 answers)
Invert "if" statement to reduce nesting
(25 answers)
Closed 3 years ago.
When writing javascript or PHP for that matter I use two ways to write if statements.
Return as soon as possible
Below I try to end the function as soon as possible. If the value is not what I expect, return it. The benefit I see here is that I don't need {} and I don't need to nest anything.
function hello(value = null) {
if(!value) return;
console.log("I'm still here!");
console.log("End of function");
}
Do something on match
In this case I don't return at all. Instead I do something if I get a "match". The upside here is that I don't need to return anything.
function hello(value = null) {
if(value) {
console.log("I'm still here!");
console.log("End of function");
}
}
So which is better? One of them or equally good?
This question already has answers here:
Javascript function fails to return object when there is a line-break between the return statement and the object?
(2 answers)
Closed 7 years ago.
I got an unexpected return value "undefined" from function 'fnExample1' if I indent code like this :
function fnExample1() {
return
1;
}
I have made this simple example : Example jsfiddle
Thanks.
write return and 1 in one line
function fnExample1() {
return 1;
}
The reason why your code returns undefined is javascript's "semicolon insertion", where a semicolon is inserted after return, and 1 is considered another statement (which is not executed)
This question already has answers here:
JavaScript plus sign in front of function expression
(4 answers)
Closed 9 years ago.
I found a strange behaviour in Javascript:
function() {
return 10;
}();
This construction doesn't work on all browser because it has a syntax error. But this construction works (returns ten):
+function() {
return 10;
}();
Why?
The + lets the js engine make the difference between this function expression and a function definition.
For more readability, we usually use
(function() {
return 10;
})();
See related article