v. carey
v. carey

Reputation: 1

.js scope program throwing error

I've looked into it and cannot figure out why this program is throwing an error in the console.

var laundryRoom = 'Basement';
var mailRoom = 'Room 1A';

function myApartment() {
  var mailBoxNumber = 'Box 3';
  var laundryRoom = 'In-unit';
  console.log('Mail box: ' + mailBoxNumber + ', Laundry:' + laundryRoom);
}

console.log('Laundry: ' + laundryRoom + ', Mail: ' + mailRoom);
console.log(myApartment());

Output:

Laundry: Basement, Mail: Room 1A
Mail box: Box 3, Laundry:In-unit
undefined

Everything seems fine until this last "undefined".

Any insight would be appreciated.

Upvotes: 0

Views: 26

Answers (2)

Ramanlfc
Ramanlfc

Reputation: 8354

myApartment() doesn't return anything explicitly, so it implicitly returns undefined. Just call myApartment() directly, not console.log(myApartment());.

Upvotes: 1

Robert I
Robert I

Reputation: 1527

myApartment is a function

The return value of a function is undefined.

Please put a return in your function.

Upvotes: 0

Related Questions