0

This is a very simple JavaScript code but I get this undefined error, I don't know what I'm doing wrong. The result I get from the following script is Great to see you, undefinedRaj. Can someone please explain what does the undefined refer to and how can I remove it.

   var greeting = function (name) {
   document.write("Great to see you," + " " + name);
   };

   document.write(greeting(name) + "Raj");
1
  • 2
    Can you try greeting("Raj"); Commented Aug 13, 2013 at 7:16

6 Answers 6

5

You have two variables called name. One is local to the function you create, the other is global.

The global one is never defined. So when you call greeting(name) you are passing name as undefined. This means that the local variable is undefined too.

Your intention was probably to:

  1. Pass "Raj" as the value of name and
  2. To not document.write the return value of the function (which, since you don't have a return statement in it, is also undefined)

Such:

var greeting = function (name) {
    document.write("Great to see you," + " " + name);
};

greeting("Raj");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping me out Quentin.
2

I believe you want to do this :

var greeting = function (name) {
    return "Great to see you," + " " + name;
};
document.write(greeting("Raj"));

1 Comment

Thanks Mohit, Quentin mentioned the RETURN element which I didn't get but your method clarified how that worked.
1

You have to pass "Raj" as a parameter to greeting method.

it should be

var greeting = function (name) {
   document.write("Great to see you," + " " + name);
};

greeting("Raj");

or

var greeting = function (name) {
   return "Great to see you," + " " + name;
};
document.write(greeting("Raj"));

In your code what is happening is first greeting() is called with global variable name which refers to window.name which is '' by default so it prints Great to see you,, and returns undefined to the called, then the document.write(greeting(name) + "Raj") is executed here greeting(name) returns undefined as said before so it prints undefinedRaj, thus the result

Comments

1

The function can't work because it return nothing, there is two way to do it:

var greeting = function (name) {
   document.write("Great to see you," + " " + name);
   };

greeting("Raj");

Or

 var greeting = function (name) {
      return ("Great to see you," + " " + name);
       };

document.write(greeting("Raj"));

Comments

0

if you want

Great to see you, Raj

as a result, you should call

greeting("Raj");

in the code you wrote, you are calling document.write(greeting(name)) but you did not define name before, so you get this "undefinedRaj" message

Comments

0

Since you've already used document.write() in the function, it is not required when you are calling the function
Cheers http://jsfiddle.net/bQcWX/

var greeting = function (name) {
   document.write("Great to see you," + " " + name);
};

greeting("Raj");

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.