Javascript Basics for Non-Developer Types pt. 3/5

Part 1 of 5, part 2 of 5.

Part 2 introduced if/else, variables, and basic functions.

For Loops

For loops at their simplest are a nice way to set up a few variables and let the computer do the work. Time saver!

A basic numerical for loop is structured:

for (var x=1; x < 8; x++) { /* code to run */; }

Beginning with for, the code between () sets up variable x. First, x is set to 1, the starting value. The second part states the value of x will be less than 8, in other words, count up to 8. The last bit is how many to increment when counting; x++ is a standard increment up of 1.

Likewise, x-- increments down by 1, and i += 5 allows us to change the value (here increment up by 5). Loops need to be properly closed or you will wind up in an endless vortex. 🌀😰


Arrays store information, like variables, except you can put more than one item in an array. Look at all this exciting stuff we can throw into an array (note numbers, strings and use of camelCase for the name):

var arrayName = [33, "cookies", 5, "cake"];

and can be printed out by, console.log(arrayName);

They can also nest:

var arrayName = [[33, "cookies"], [5, "cake"]];

Using Arrays and For Loops Together

Arrays + Loops can be very helpful. First, set up an array:

var junkFood = ["cheeseburgers", "fries", "chips", "queso"];

Then, write a for loop to print out items in the array in a sentence about tasty these things are.

for (var x = 1; x < junkFood.length, x++) { console.log("I sure could go for some " + junkFood[x]); }

Will print to the console:
I sure could go for some cheeseburgers. 🍔
I sure could go for some fries. 🍟
I sure could go for some chips.
I sure could go for some queso.


While Loops

Let's say you don't know when exactly you want the loop to run. While Loops operate under a condition:

while (condition) { /* do something */; };

As long as the loop is true (1), it will run, if false (0), it will cease. Again, be careful about endless loops! Set up the condition to close the loop at some point by:

var condition = true;

while (condition) { console.log("Rad!"); condition = false; }

Do/While Loops

Do tells the loop do run this code once, then check the condition to see if it needs to run again (like a normal While loop):

var condition = true;

do { console.log("Doing the Do Loop"); } while (condition);


Switch Statements follow a logical pattern where an expression is declared between (), and the computer will run code within the statement until it matches that expression. If it doesn't find a match, a default statement is run.

switch(expression) { case 'option1': /* do this*/; break; case 'option2': /* do this*/; break; case 'option3': /* do this*/; break; default: /* do this*/; }

Logical Operators

Logical operators can be used to scan through multiple variables. They are:

&& and

|| or

! not

Here are a few variables and an if/else statement:

var happy = true;

var dance = true;

var finished = function () { if (happy && dance) { return true; } else { return false; } };

Since both variables are true, the statement is true.

Read on to Part 4.