0

When I call reality() I want to print Royal Blue but I am getting Royal undefined I am just a beginner in JavaScript Please help me find out what is the problem.

var color = "Blue";

function reality() {
  var color = "Royal " + color;
  console.log(color);
}
reality();
4
  • Define your variable color inside function reality Oct 20, 2016 at 6:40
  • Remove var from color inside function as you already define color as global variable
    – R.K.Saini
    Oct 20, 2016 at 6:41
  • Please have a read of the duplicate target. Understanding how variable scope works is crucial when working with JS.
    – Cerbrus
    Oct 20, 2016 at 6:44
  • var color = "Blue"; function reality() { color = "Royal " + color; console.log(color); } reality();
    – Hope
    Oct 20, 2016 at 6:45

2 Answers 2

4

Please help me find out what is the problem.

In JavaScript, when all the variables declared with var, will have the default value of undefined unless explicitly something is assigned.

In your case,

var color = "Royal " + color;

you are using color to define the variable color itself. So,

"Royal " + color

will become

"Royal " + undefined

That is why the result is `Royal undefined.


Basically, the variable color declared within the reality shadows the variable, declared outside the scope of that function. So, you need to choose a different name.

var color = "Blue";

function reality() {
  var realityColor = "Royal " + color;
  console.log(realityColor);
}
reality();

will print

Royal Blue

Note: If you actually meant to modify the color variable declared outside, then you should not define a variable called color in reality and instead, you should access it directly, like this

var color = "Blue";

function reality() {
  color = "Royal " + color;
  console.log(color);
}
reality();

But, this function doesn't return anything. To make it better, you can return the value and assign it the variable, like this

var color = "Blue";

function reality() {
  return "Royal " + color;
}

color = reality();
console.log(color);
5
  • Thanks, for this very detailed answer, I am learning about global vs local variable and this is where the question come from. If I don't write "var" in front color inside the function reality then it will be a global variable, I want the color variable value as "Blue" globally and "Royal Blue" inside the function reality. is it possible? For example: console.log(color); going to print Blue and reality() going to pring Royal Blur
    – imondal007
    Oct 20, 2016 at 7:11
  • @imondal007 It will not become a global variable immediately. What happens is, when you try to access a variable, JavaScript will look for its declaration in the current scope, if it is not there, it will go to the scope above, if not there, above that. It goes on, till it reaches the global scope. Oct 20, 2016 at 7:13
  • @imondal007 If all you care about is printing, then just do console.log("Royal " + color) without any declaration statement Oct 20, 2016 at 7:14
  • Printing is not the issue, I really asked this question from learning more about the use of local & global variable. I think I need to know more about JavaScript to understand what you guys are talking about. Thank you again :)
    – imondal007
    Oct 20, 2016 at 7:17
  • Now I understand, Local variable gonna ignore the existence of any other variable of same name :)
    – imondal007
    Oct 20, 2016 at 7:51
1
var color = "Blue";
function reality() {
  color = "Royal " + color;
  console.log(color);
}
reality();

Color is defined globally, so can be changed inside a function, however if you are defining again it takes null which is undefined.

2
  • It that case, Royal Blue become the value of global variable. I want this only inside reality function not globally
    – imondal007
    Oct 20, 2016 at 7:13
  • in that case you define one more local variable called _color and assign the global values, so that blue will be global, and royal blue will be local.
    – Raki
    Oct 20, 2016 at 7:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.