Ajax-loader-large-white
Getting Started with Programming
Decisions, decisions

Nice work on comparisons! Now let's see how we can use comparisons to ask yes or no questions.

Say we want to write a program that asks whether your name is longer than 7 letters. If the answer is yes, we can respond with "You have a long name!" We can do this with an if statement:

if( "myName".length >= 7 ) {
    console.log("You have a long name!");
}

An if statement is made up of the if keyword, a condition like we've seen before, and a pair of curly braces { }. If the answer to the condition is yes, the code inside the curly braces will run.

Instructions

Check out the if statement in the editor.

  1. On line 1, add a condition inside the parentheses ( ).
  2. If the answer to the condition is yes, the code inside the curly braces will run. So on line 2, use console.log to print out a string.
?
Stuck? Get a hint! Hint
Get live, fast support from Codecademy Advisors when you get stuck. Learn more.

You can write a condition with a comparison like 10 > 5:


if (10 > 5) {
    // This part will run, since 10 > 5
}
script.js