3

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

@Abdel-RahmanShoman, no worries, I updated my answer to include why undefined is printed, please have a look.
1

Try this

brightRoom('bedroom');

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

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.