Why this code result is undefined? [duplicate] - javascript

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 5 years ago.
function aaa() {
return
{
test: 1
}
}
console.log(aaa());
Why this code result is undefined?
I assume is will be object.

Because when you change the line it terminate the statement so you have to put curly bracket after return to make it work.
Just use this:
function aaa() {
return {
test: 1
}
}
console.log(aaa());

Related

need help, conditional if and problems with variables, return empty [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I have this code in PHP and it works excellent, I am passing it to node (javascript) but it is giving me problems, it does not expect to terninate the conditionals and continues with the process, it gives me as a result the empty variable.
var variable = "";
if (typeof req.body.princp !== 'undefined') {
if (iduser == idperprincp ) {
var sqlfav = `SELECT * FROM tblfavo WHERE idusr = '${iduser }' ORDER BY id DESC`;
mysqlcon.query(sqlfav, function (err, refavsql) {
if (err) throw err;
if (refavsql.length > 0) {
variable = "restul 1"
} else {
variable = "restul 2"
}
})
} else {
variable = "restul 3"
}
} else {
variable = "restul 4"
}
console.log(variable) // Result empty..
because it always returns the bariable empty, the same code in PHP if it works well, apparently, in node (javascript) first prints the variable and then does everything within the conditional, how do you solve that problem?, thank you very much

What is the effect of new line after return keyword [duplicate]

This question already has answers here:
Automatic semicolon insertion & return statements [duplicate]
(3 answers)
return and newlines?
(3 answers)
Why do results vary based on curly brace placement?
(6 answers)
Closed 4 years ago.
I have an iife & a inner function.The inner function is returning an object in first case the { is immediately after the return keyword, in second case the brace { starts in a new line. In fist case the value is correctly returned and in second case it is throwing an undefined error
(function() {
function sayHello() {
var name = "myName";
return {
fullName: name
}
}
console.log(sayHello().fullName);
})();
(function() {
function sayHello() {
var name = "myName";
return
{
fullName: name
}
}
console.log(sayHello().fullName);
})();
I am not able to understand the effect statement in a new line after return statement & I am only guessing the return statement is getting terminated even though there is a statement in next line

behavior of javascript 'this' keyword in closure [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 4 years ago.
Consider a simple pattern where we call setTimeout within a loop to print the loop counter:
function g(i){
return function()
{
console.log(i);
}
}
for(i=0;i<4;i++)
{
setTimeout(g(i),3000);
}
This returns the expected result:
0
1
2
3
According to my understanding, this function should do the same thing
function f(i){
this.n = i;
return function()
{
console.log(this.n);
}
}
for(i=0;i<4;i++)
{
setTimeout(f(i),3000);
}
Instead, I get varying results, in NodeJS:
undefined
undefined
undefined
undefined
And in Google Chrome:
3
3
3
3
Neither of these results make sense to me, so I was hoping someone else can explain this to me.
You can use an arrow function to keep your this:
function g(i){
return () =>
{
console.log(i);
}
}
for(i=0;i<4;i++)
{
setTimeout(g(i),3000);
}

Why is this javascript function returning undefined? [duplicate]

This question already has answers here:
Javascript function fails to return element
(2 answers)
Object.keys forEach returns undefined
(2 answers)
Function with forEach returns undefined even with return statement
(5 answers)
ES6 class methods not returning anything inside forEach loop
(2 answers)
Why does this forEach return undefined when using a return statement
(5 answers)
Closed 4 years ago.
Why is this function returning undefined? It has a value inside the function, but once I try to assign it to a new variable, it comes back as undefined.
function getLookupDefault(lookupModel) {
Object.keys(lookupModel.LookupValues).forEach(function (key) {
if (lookupModel.LookupValues[key].IsDefault == true) {
test = lookupModel.LookupValues[key].Name;
console.log("test: " + test);
return test;
}
})
};
var tst = getLookupDefault(model.LookupValuesDelimiter);
console.log("tst: " + tst);
Edit:
Thank you. Coming from c#, this was not obvious to me. I have edited the code to this and it works correctly.
function getLookupDefault(lookupModel) {
for (var key in Object.keys(lookupModel.LookupValues)) {
if (lookupModel.LookupValues[key].IsDefault == true) {
test = lookupModel.LookupValues[key].Name;
console.log("test: " + test);
return test;
}
}
}
That return statement you have in there doesn't return a value to the outer function, it only returns a value to the inner function called by forEach.

Node.js Function Returns 'undefined' [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
The following Node.js code prints 'undefined', even though it found the file.
var fileFound = function() {
fs.readFile('public/images/acphotos/Friedrich-EL36N35B.jpg', function(err, data) {
if (err) {
console.log(err);
return false;
} else {
return true;
}
});
}
console.log("Return value: " + fileFound());
How would I rewrite it? I don't fully understand the solution in the other thread I was shown.
Because the return statements are inside the callback passed into fs.readFile.
the fileFound function never returns anything, therefore you get undefined.

Categories

Resources