If(!programming): What Does It Exactly Mean? - Codecademy

Skip to ContentProfile image of hojarasca_Submitted by hojarasca_over 11 yearsif(!programming): what does it exactly mean?

Hello, first time writing here :)

I think I’ve read most of the questions and answers about this topic, but I think I still don’t understand. This code is working, but I don’t understand the !programming.

var programming = false; var happy = function() { if(!programming) { return(true); } else { return(false); }; };

But first I have another question. Up until now, in the exercises, I would declare a variable and assign it the value true, and then write something like if(varName) which would mean if(varName === true). My question is: was that because the variable had been assigned the value true or because if(varName) means if(varName === true)?

And, the second question: how would you translate if(!programming) to regular English? I know that if programming = false, then !programming = true. But how does that make sense inside the if statement? Does it mean if no programming, that is to say, if programming is false?

ADDED I wrote this to figure out the first question. I guess is the same as before, but simpler.

var programming = false if(programming) { console.log(1); } else { console.log(2); };

This prints 2, so I guess if(varName) means if(varName=== true)?

Answer 5411c73f80ff33353d00284e

0 votes

Permalink

Actually the argument of if() has to be a boolean expression or in other words evaluate to true or false. You could put whatever you want inside, even if that does not makes sense, provided it results into a boolean expression. Following are valid boolean expressions:

true varName varName=== true ! varName

The way to understand the “!” operator is to think that as a “not”, so when you ask if(!programming) is a way to express “if it does not program do such”….

Profile image of javaPlayer07327Submitted by javaPlayer07327over 11 years

5 comments

Profile image of hojarasca_Submitted by hojarasca_over 11 years

Thank you, thank you!

Profile image of DanoolSubmitted by Danoolover 11 years

i dont get it. what do you mean “If it does not program do such”

Profile image of DanoolSubmitted by Danoolover 11 years

if i set var programming = false; what does if (programming) mean and what would if(!programming) mean? would if (!programming) in this case , with it set as false mean if(programming===!false===true) ?

Profile image of DanoolSubmitted by Danoolover 11 years

ah i think i get it sorry for talking that much

Profile image of AliceRosierSubmitted by AliceRosierover 11 years

talking that much helps the rest of us understand it too :)

Answer 5412d129282ae33aff0017f5

0 votes

Permalink

The ! is the not operator, so it means if “not false” which means true or if “not true” which means false. Here is a list how other values evaluate to booleans if used in a condition: http://www.sitepoint.com/javascript-truthy-falsy/

And last but not least varName === true and varName behave the same way so you can use the shorter version as you can see by getting through the possible results:

if varName is true and varName === true is true if varName is false then varName === true is false

So to see varName === true has for any choices of varName the same value as varName itself.

Profile image of haxor789Submitted by haxor789over 11 years

Answer 54248282282ae3eca60001e4

0 votes

Permalink

The way you can interpret it like this: When you enter

hey var = true; if (hey) // is a way of if saying hey === true { console.log (“hello”); // The condition is true so you’ll print this post } else { console.log (“bye”); }

If the condition is false: hey var = false; if (hey) // is a way of saying if hey === true { console.log (“hello”); } else { console.log (“bye”); // The condition is false so you will print this message }

But if you enter:

hey var = true; if (!hey) // You’re telling the system that hey === true but want to change it to false or vice versa if originally you condition is hey === false change it to true { console.log (“hello”); } else { console.log (“bye”); //As initially var === true you will change it to false and will print this message }

Profile image of ssaaiilleSubmitted by ssaaiilleover 11 years

1 comments

Profile image of haxor789Submitted by haxor789over 11 years

its var hey = true not hey var = true. var is the keyword to get a variable followod by the name of that variable you want to create. The rest is good.

Popular free courses

  • Free course

    Learn SQL

    In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Checker DenseBeginner Friendly.4 Lessons
  • Free course

    Learn JavaScript

    Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Checker DenseBeginner Friendly.11 Lessons
  • Free course

    Learn HTML

    Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
    • Checker DenseBeginner Friendly.6 Lessons
Explore full catalog

Tag » What Does It Mean If