function a(){
return{
bb:"a"
}
}
and
function a(){
return
{
bb:"a"
}
}
Is there any difference between the two code, if yes please explain.
function a(){
return{
bb:"a"
}
}
and
function a(){
return
{
bb:"a"
}
}
Is there any difference between the two code, if yes please explain.
The difference is huge. The first returns an object. The second - undefined due to Automatic Semicolon Insertion. return
will become return;
function a(){
return{
bb:"a"
}
}
function a1(){
return
{
bb:"a"
}
}
console.log(a(), a1())
For some reason, the Javascript bods decided that a single return
on a line will be subject to a sort of "auto-correct" mechanism called Automatic Semicolon Insertion.
So your second snippet becomes
function a1(){
return;
{
bb:"a"
}
}
which is no longer syntactically valid!
Reference: What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(I'm currently learning Javascript myself and have already fallen for this.)