A.Shoman
A.Shoman

Reputation: 3085

Pass string to JavaScript function results in undefined

So what I am trying to do is very simple and straightforward. Passing a String to a JavaScript function and print it as following.

brightRoom('bedroom');

Then

function brightRoom(room){
 console.log("bright " . room);
}

What I get is undefined. Which I am not sure what may caused that issue.

Upvotes: 3

Views: 2095

Answers (2)

robbmj
robbmj

Reputation: 16496

JavaScript's concatenation operator is a +

brightRoom('bedroom');

function brightRoom(room) {
  // of course console.log("bright " + room); works
  document.write("bright " + room);
}

It results in undefined because the JavaScript engine thinks that you are trying to find a property of the string called room which of course does not exist. When you try to access a non existent property of an object undefined is returned

Upvotes: 1

James
James

Reputation: 16339

Try this

brightRoom('bedroom');

function brightRoom(room){
 console.log("bright " + room);
}

Upvotes: 1

Related Questions