-1

Coming from Python, learning JavaScript:

var test = 1;
var test2 = function(arg) {console.log(arg);};

When I execute var test3 = test2(test); in console, I get as a response:

undefined
1

But when I re-enter test3 or console.log(test3) I'm getting the undefined response inestead of 1. I tried to return value in function instead:

var test2 = function(arg) {return(console.log(arg));};

and

var test2 = function(arg) { var text = console.log(arg); return text;};

But I'm still getting the same response. How to 'bind' result of the function test2 to variable test3?

Thanks.

2
  • You're actually getting the result, but the default return value for any function in javascript is undefined, and that goes for console.log() as well. Commented Jul 15, 2014 at 16:34
  • Because the function does not have a value. To add one use return. For example: function(arg) {console.log(arg); return arg;} Commented Jul 15, 2014 at 16:35

3 Answers 3

1

console.log returns undefined.

var test2 = function(arg) { 
    var text = console.log(arg); // text === undefined
    return text;
}; // returns undefined

// Then ...

var test3 = test2(test); // test3 === undefined
Sign up to request clarification or add additional context in comments.

Comments

1

console.log always returns undefined

If you did return arg instead of returning what console.log returns, then you'd be good.

Comments

1

You are not setting test3 to anything. Console.log does not return anything(except undefined).

If you want to set it to the value of arg, just return the value itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.