Joe Rizzo
Joe Rizzo

Reputation: 17

Why do I get undefined?

Will someone answer a small question for me that I simply do not understand; why is it that the output of this function is undefined? I expected to get "cheese whiz". A plain answer would be immensely helpful to me towards understanding JS basics.

var x = "cheese ";
var y = "whiz";

function cheeseWhiz (x,y){
  console.log (x+y);
}

enter image description here

Upvotes: 0

Views: 205

Answers (7)

romellem
romellem

Reputation: 6491

OP is using REPL.IT, and it looks like it always outputs what gets returned by the last function call if they don't redirect that function output to a variable

Example 1:

Hello World

Example 2:

World

So even though I am calling a function three times, the console on the right shows the output from the last function that doesn't have its output saved.

In OP's case, they never actually call their function, so it shows undefined as the last return value from the last function call (aka, from nothing).

Even if they did run cheeseWhiz, they'd see the console.log value, but they'd still see a return value of undefined.

Example 3:

Cheese Whiz

So to answer the original question, assuming that "output" means "return value," the return value of the cheeseWhiz function is undefined because it never returns anything!

If you'd like it to return something, so that you can use the value later on, you'd want to use a return statement, rather than just outputting the string to the console.

var x = "cheese ";
var y = "whiz";

function cheeseWhiz(x, y) {
  return x + y;
}

var result = cheeseWhiz(x, y);

// Do whatever you want with `result`, like output to the console
console.log(result);

Upvotes: 0

Lyubomir
Lyubomir

Reputation: 20027

Because you need to call the function.

var x = "cheese ";
var y = "whiz";

function cheeseWhiz (x, y) {
  console.log (x+y);
}

cheeseWhiz(x, y);

Also, you are likely running the code in the console which prints the result of the last expression/statement. Because the last expression was a function declaration, no result was returned, therefore the console yields undefined.

@Felix Kling added the missing piece to this answer.

calling cheeseWhiz(x, y); will also "output" undefined because the function doesn't return anything and the console prints the value of the last expression/statement.

Upvotes: 5

Patrick Denny
Patrick Denny

Reputation: 290

the x & y arguments in your cheezeWhiz function create locally scoped variables.

var x = "cheese ";
var y = "whiz";

function cheeseWhiz (x,y){ //creates new x,y variables in local scope
   console.log (x+y);
 }
 function cheeseWhiz2 (){ 
   console.log (x+y); //will move to outer scope to find x & y variables
 }

 cheezeWhiz(x,y) // this passes in the outer x & y. will log 'cheezewhiz'
 cheezeWhiz('peanut','butter') // this will log 'peanutbutter'
 cheezeWhiz2() // gets outer x & y. will log 'cheezewhiz'
 cheezeWhiz2('peanut','butter') // also gets outer x & y. will log 'cheezewhiz'

here's a good run down on JavaScript Scope http://ryanmorr.com/understanding-scope-and-context-in-javascript/

Upvotes: 0

olivarra1
olivarra1

Reputation: 3399

If you are calling this function and wondering why are you getting undefined back is because it actually doesn't return anything, it just console.logs.

var x = "cheese ";
var y = "whiz";

function cheeseWhiz (x,y){
  return x+y;
}
console.log(cheeseWiz(x,y));

Upvotes: 2

Oliver Watkins
Oliver Watkins

Reputation: 13489

You are not calling the function. Try this :

var x = "cheese ";
var y = "whiz";

function cheeseWhiz (x,y){
  console.log (x+y);
}
cheeseWhiz(x,y);

Upvotes: 1

phortx
phortx

Reputation: 909

The reason you're getting undefined is because you're defining a function, but you don't call it actually.

Change to:

function cheeseWhiz (x, y){
    console.log (x + y);
}

cheeseWhiz("cheese ", "whiz");

Upvotes: 0

Naqeeb Sial
Naqeeb Sial

Reputation: 667

You have to call function it will not call by itself like cheeseWhiz (x,y);

Upvotes: 0

Related Questions