Press key to advance. Zoom in/out: Ctrl or Command + +/-.

Basic JavaScript

Estelle Weyl

Conditional Statements

if/then

if (condition is true) {
    // then do this ....
}
if(2 + 2 == 4) {
    console.log("You are a mathmatician!");
}
if(prompt("Your name?") != "Estelle"){
    console.log("You are an imposter!");
}

Comparison Operator

OperatorDefinition
==Equal to
===Equal to & same type
!=Not Equal to
>Greater than
>=Greater or equal to
<Less than
<=Less or equal to
!Not
&&AND
||OR

Comparing Case Insensitive Strings

var firstName = prompt('First Name?');
  if(firstName.toUpperCase() === 'ESTELLE') {
    alert(firstName);
  }
var errMsg,
     email1 = prompt('Your email address:');
     email2 = prompt('Re-enter email address:');

  if (email1.toUpperCase() != email2.toUpperCase()) {
  	errMsg += "Email addresses do not match\n";
  }

Conditional Examples

if(5 > 4)
if("Clinton" > "Bush")
if(5 == '5')
if(0 < x && x < 10)
if(!firstName)
if(nincompoops.length == 1)
if(isNaN(fruitcake))

Comparing Case Insensitive Strings

var firstName = prompt('First Name?');
if(firstName.toUpperCase() === 'ESTELLE') {
   alert(firstName);
}
var errMsg,
    email1 = prompt('Your email address:');
    email2 = prompt('Re-enter email address:');

if (email1.toUpperCase() != email2.toUpperCase()) {
	errMsg += "Email addresses do not match\n";
 }

Comparing Numbers

var age = parseInt(prompt('Lie about your age?'));
if(isNaN(age)) {
  // not a number!! 
  var banana = 'frog'
} 

var errMsg,
email1 = prompt('Your email address:');
email2 = prompt('Re-enter email address:');

if (email1.toUpperCase() != email2.toUpperCase()) {
errMsg += "Email addresses do not match\n";
}

Otherwise ...

if ... else if .... else

var msg,
    age = +prompt("how old are you?");
    
if(!age || isNaN(age)) {
  msg = "You're too old to tell us your age?";
} else if (age <= 18 || age >= 65) {
  msg = "Kudos to you for getting mad skillz";
} else if (age > 45) {
  msg = "You should have lied about your age.";
} else if (age < 25) {
  msg = "You're too young to rent a car,\
  but old enough to rock JS";
} else {
  msg = "There's nothing wrong with being average.";
}

Loops

while (condition is true) {
    // do these things
    // alter the condition       
}
var counter = 0, iterations = 4;
while (counter < iterations) {
    // do these things
    counter++;
}

Declared in one line

for(var counter=0; counter < iterations; counter++) {
    // do these things
}

Iterate even if condition is never met

do while

var count = 7;
do {
    console.log(count);
    count++;
} while (count < 5);

Will execute code block at least once, even if condition is never met.

Don't forget to increment your counter or your browser will crash

infinite loop

Exercises

  • Is your user old enough to vote? Drink? Rent a car? Run for president? Retire?
  • Multiplication Table: pick a number? How many multiplication values do you want?
    Example input: 9, 3.
    Example output: 9 x 1 = 9, 9 x 2 = 18, 9 x 3 = 27
  • If you used the while loop for the above, do it with the for loop. If you used the for loop, rewrite it as a while loop.
  • Have the user guess your number. Tell them they're warm if close or cold if far off.

Next

Go