Learn JavaScript - Conditionals, Loops, Array

Question Click to View Answer

What does the following expression return?

4 > 1;
true

4 is greater than 1, so the expression evaluates to true. 4 and 1 are referred to as the operands and > is the operator.

What does the following expression return?

"chatty" === "chatty";
true

The "chatty" strings both have the same length and the same elements in the same order, so the === operator returns true.

What does the following expression return?

3 <= 300
true

The number 3 is less than or equal to 300.

Print the string "I just ate a lot" to the console.

console.log("I just ate a lot");

if statements will execute the code in the block if the boolean condition is true. What does the following expression print to the console?

if (5 === 5) {
  console.log("This code is executed");
}
"This code is executed"

The boolean condition 5 === 5 evaluates to true, so the code in the block is executed. The code block is delimited by {}.

What does the following expression print to the console?

if (5 > 10) {
  console.log("Not so sure about this");
}

Nothing is printed to the console because the boolean condition is false. When the boolean condition is false, the code in the code block is not executed.

What does the following expression print to the console?

if (5 > 10) {
  console.log("Not so sure about this");
} else {
  console.log("walking dead");
}
"walking dead"

When the boolean condition evaluates to false (5 > 10), the code block associated with the if keyword is not executed and the code block associated with the else keyword is executed.

What does the following expression print to the console?

if ("candy" === 8) {
  console.log("do something with your life");
} else if ("blah" === "blah") {
  console.log("just chill, have fun");
} else {
  console.log("people are strange");
}
"just chill, have fun"

is printed to the console as the boolean condition associated with the else if clause is true.

What does the following expression print to the console?

if (5) { console.log("I like peanuts"); }

"I like peanuts" is printed to the console. 5 does not equal true or false, but it is considered "truthy" in a boolean context. Type Boolean(5) in the console to see if the number 5 is considered truthy or falsey. In JavaScript, undefined, NaN, null, 0, "", and the keyword false are all false in a boolean context. All other values are truthy.

What doe the following expression print to the console?

if ("") {
  console.log("program more");
} else if ("cool") {
  console.log("party hard!");
} else {
  console.log("blah");
}
"party hard!"

"party hard!" is printed to the console because "cool" is truthy. "program more" is not printed to the console because the empty string is falsey.

What does the following expression return?

!false
true

The ! operator returns the opposite of the boolean value it is prepended to.

What does the following expression print to the console?

if (!undefined) console.log("This syntax is weird…");
"This syntax is weird..."

!undefined is true in a boolean context, so the code block associated with the if statement is executed. The code block is not delimited with {} because the curly brackets are optional when the code block is on the same line as the if statement. For multi-line code blocks, the curly brackets are required.

What does the following code print to the console?

var counter = 0;
while (counter < 3) {
  console.log("The counter is at " + counter);
  counter++
}
The counter is at 0
The counter is at 1
The counter is at 2

A while loop will execute the block as long as the boolean condition evaluates to true.

What does the following code print to the console?

var result = 0;
for (var i = 0; i < 5; i++) {
  result += i;
}
console.log(result);
10

The for loop initializes a loop variable, specifies when the loop should terminate, and increments the loop variable. For loops are a convenient way to loop through arrays.

Arrays are ordered collections of elements. What does the following code print to the console?

var my_array = [1, "bob"];
console.log(my_array[1]);
"bob"

The first statement initialized the array and assigns it to the variable my_array. Arrays are zero-indexed, so the first element is in position zero, the second element is in position one and so forth. The [] syntax is used to access elements in arrays.

What does the following code print to the console?

var q = "quack";
var num = 555666;
console.log([num, q, true]);
[555666, "quack", true]

Variables can be used to represent elements in an array. Also notice that arrays can contain elements with different types. The array in this example contains one number, one string, and one boolean value.

What does the following code print to the console?

["uno", "dos", "tres"].length;
3

All arrays have a length property which represents the number of elements in an array.

Create an array with the following elements: "bob", 23, false.

["bob", 23, false];
// OR
new Array("bob", 23, false);

Both answers create arrays with the same elements, but the first example uses Array literal notation and the second example uses a constructor function. Other quizzes will cover constructor functions in detail.

Print the last element of my_array to the console.

var my_array = ["horse", "pig", "cow"];
console.log(my_array[2]);

What does the following code print to the console?

var writing_tools = ["pencil", "pen", "marker"];
console.log(writing_tools[writing_tools.length - 1]);
"marker"

writing_tools.length evaluates to 3 and writing_tools.length - 1 evaluates to 2. The final element of the array has an index equal to the length of the array minus one.

What does the following code print to the console?

var b = [1, 2];
b[0] = "master p";
console.log(b);
["master p", 2];

Array elements can be updated by indexing an element of the array and setting the value to something new.

What does the following code print to the console?

var numbers = [2, 4, 6, 8];
var counter = 0;
var sum = 0;
while (counter < numbers.length) {
  sum += numbers[counter];
  counter++;
}
console.log(sum);
20

The while loop iterates over every element in the numbers array and updates the sum variable with each iteration.

What does the following code print to the console?

var words = ["teenage", "mutant", "ninja", "turtles"];
var great_show = "";
for (var i = 0; i < words.length; i++) {
  great_show += words[i] + " ";
}
console.log(great_show);
"teenage mutant ninja turtles "

The great_show variable is initially assigned to the empty string and is updated with the strings in the word array plus a space for each iteration of the for loop.

What does the following statement return?

typeof(["cats"]);
"object"

Arrays are a special type of object. Later quizzes in this series will cover objects in greater detail.

What does the following statement return?

[1, 89] === [1, 89];
false

The two arrays being compared with the === operator have the same elements, but they are different arrays, so false is returned. The === only returns true when comparing arrays if the operands are exactly the same array.

What does the following code return?

var blue = ["da ba dee da ba die"];
var green = blue;
blue === green;
true

In this example, the === operator is comparing two variables that point to the same exact array, so true is returned.

Sum all the elements in the following array if they are numbers, otherwise ignore the element. Print the result to the console.

var weird = [1, 40, "bob", [], false, 89];
var result = 0; 
for (var i = 0; i < weird.length; i++) {
  var element = weird[i];
  if (typeof(element) == "number") { result += element };
}
console.log(result);

All elements from the weird array are looped through and added to the result variable if they are numbers.

Update the "shots" element in the following array to be "don't do it".

var beverages = ["beer", "wine", "shots"];
beverages[2] = "don't do it";

What are the falsey values in JavaScript? What does it mean when a value is falsey?

The falsey values are the empty string, 0, the keyword false, undefined, and null. In a boolean context, falsey values are treated as false (falsey values don't necessarily equal false).

Concatenate all the elements in the following array that begin with the letter "b" and print the resulting sentence to the console.

var heaven = [34, [], "bodacious", "barbecues", "begin", "whatever", "by", "battering", true, "bacon"];
var result = "";
for (var i = 0; i < heaven.length; i++) {
  var element = heaven[i];
  if (element[0] == "b") { result += element + " " };
}
console.log(result);

Strings can be indexed, similar to arrays.