Bootcamp
Search…
0.1: New JS Syntax

Introduction

In Coding Basics we omitted some best-practice JS syntax to focus on foundational concepts. We introduce these best practices in Coding Bootcamp for professional software engineering.

+= 1

You can use x = x + 1 , x += 1 , or x++ to increment the value of variable x by 1. ESLint recommends against using ++, so we default to += 1 for brevity.
var dice = 5;
var kilos = 5;
var minutes = 5;
​
// Old
dice = dice + 1; // 6
// New, += is recommended
kilos += 1; // 6
minutes++; // 6

for loops

By now we should be relatively familiar with the mechanics of loops. From now onward, we will use for loops for loops with a fixed number of iterations.

Old Way (While Loop)

In Coding Basics we used while loops everywhere.
// initialize loop counter
var counter = 0;
​
// run 10 times
while (counter < 10) {
// main part of the loop
console.log(`current counter: ${counter}`);
​
// increment always the last line in the loop
counter += 1;
}

New Way (For Loop)

For a fixed number of iterations, for loop syntax is more concise.
// initialize loop
for (var counter = 0; counter < 10; counter += 1) {
// main part of the loop
console.log(`current counter: ${counter}`);
}

Shorter Counter or Index Variable Name

It's quite common to shorten the name of our counter variable name to i, where "i" is short for "index". If your index also represents something more specific, use the more specific name, but i is a generally accepted short name for an index in a loop. If you have nested loops, j is the standard name for the index in the nested loop.
// initialize loop
for (var i = 0; i < 10; i += 1) {
// main part of the loop
console.log(`current counter: ${i}`);
}

Strict Equivalence (===)

We have been using == to test equivalence in our boolean statements.
When testing values, == does not take data type into account. This means that it is more forgiving and less work to check / correct for data type.
3 == "3"; // true
true == "true"; // true
Some equivalence checks are not intuitive for what are referred to as truthy or falsy values.
0 == false; // true
true == "true"; // true
"" == null; // true
We should default to not allowing any of the above to be true. Your code is less likely to have errors if the conditional is helping check the data and the data type.
3 === "3"; // false
true === "true"; // false
0 === false; // false
true === "true"; // false
"" === null; // false
To make examples like these evaluate to true we can cast the variable value into the correct type:
Number("3") === 3; // true
Boolean("true") === true; // true

Exercises

  1. 1.
    Find an old exercise from Coding Basics
  2. 2.
    Update your code with new code for each of the above syntax changes
  3. 3.
    Make your code run without errors
When making comparisons with user input in the Coding Basics starter code, logic like input === 3will not evaluate to true, because input is of type string.
Copy link
On this page
Introduction
+= 1
for loops
Old Way (While Loop)
New Way (For Loop)
Shorter Counter or Index Variable Name
Strict Equivalence (===)
Exercises