Jump to content

console.log does not print


hisoka
 Share

Recommended Posts

in this script

 

var x = "b";
var y = "a";
var z = "c";
if(x==y)
{
if(x==z)
{
console.log( "equal");
}
else
{
console.log("not equal");
}
}

 

console.log should print in the screen "not equal " but instead it gives me " undefined" . Why it gives undefined instead of printing "not equal" ? and how to make it print "not equal"?

Link to comment
Share on other sites

If you're running this script straight in the console then it shows undefined first because that code does not return anything.

 

From the look of that code, it should never write anything to the console because x is not equal to y. Here's the code properly indented so that you can see that the code will do nothing unless x and y are the same.

var x = "b";
var y = "a";
var z = "c";
if(x==y)
{
  if(x==z)
  {
    console.log( "equal");
  }
  else
  {
  console.log("not equal");
  }
}
Link to comment
Share on other sites

"first because that code does not return anything"

 

it will return either equal or not equal !!! is not it ? Could you please explain ?

 

 

 

 

"From the look of that code, it should never write anything to the console because x is not equal to y"

 

but there is the

 

 

else
{
console.log("not equal");
}

 

so normally if they are not equal then the condition is false and else block is executed . Thus , normally, the console log should print " not equal" what do you think ?

Link to comment
Share on other sites

No, that else is inside an if() statement that never is executed.

 

Here's your code:

if(x == y) {
  ... All console.log() calls here, even the else statement is inside here
}

If x is not equal to y then console.log() is never called.

 

When I say it doesn't return anything I mean that the code does not evaluate to a value. In the console you can write Javascript expressions, such as x == y and the console will show the value to expression evaluates to (it would display "true" for the previous expression). If there is not an expression in your code it will just show undefined, which is not a problem. The problem in your code is that console.log() is never being called and I've already explained why.

  • Like 1
Link to comment
Share on other sites

Put it in a block of code that actually gets executed. I don't know how you have so much trouble understanding a simple if() statement. Please read the Javascript tutorial on W3Schools.com

  • Like 1
Link to comment
Share on other sites

what about this :

 

var temp = 40;

if (temp < 70)
{
if (temp < 30)
{
console.log("< 30");
}
else
{
console.log(" between 30 and 70");
}
}

 

in comparison with this

 

var x = "b";
var y = "a";
var z = "c";
if(x==y)
{
if(x==z)
{
console.log( "equal");
}
else
{
console.log("not equal");
}
}

 

They are approximately the same . The first gives between 30 and 70 as a result meanwhile the other gives undefined ???!

Edited by hisoka
Link to comment
Share on other sites

If statements can be nested because any code is allowed within an if statement, even another if statement.

 

Else statements always follow an if statement. The fact that it is inside another if statement in your code is just due to how the code is designed.

 

Properly indenting code is important so that you can know which if statement the else belongs to.

Link to comment
Share on other sites

They are approximately the same . The first gives between 30 and 70 as a result meanwhile the other gives undefined ???!

Yes, they are the same in terms of structure. The difference is in the evaluation of the criteria in the if statements. I'm not sure how much clearer it can be explained. Like Ingolme said, indentation is important to help you keep track of what code is in each block.

If we look at your code (properly indented):

var x = "b";

var y = "a";

var z = "c";

if(x==y)

{

if(x==z)

{

console.log( "equal");

}

else

{

console.log("not equal");

}

}

 

Everything between the red brackets is one block of code and will be executed together. So when it checks the condition if(x==y) it will run that code only if x is equal to y and since they're not ("b" is not equal to "a") and you have no else statement to match that block, nothing gets executed. Your other example can be formatted exactly as above, but the condition is different. In that one, the condition evaluates to true (after all, 40 is indeed less than 70) and so the code in the block is then executed. If you change the value of temp to anything greater than 70, you will get the same result as in your first code (that is, nothing). In order to get output under every single instance, you would need to add another else block to go with the first if statement:

if(x==y)

{

if(x==z)

{

console.log( "equal");

}

else

{

console.log("not equal");

}

}

else

{

console.log("not equal");

}

Edited by ShadowMage
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...