Categories
JavaScript Basics

Highlights of JavaScript — Conditions and Arrays

Spread the love

To learn JavaScript, we must learn the basics.

In this article, we’ll look at the most basic parts of the JavaScript language.

Testing Sets of Conditions

We can test more than one condition with an if statement.

To combine them, we can use the && or || operators.

&& is the logical AND operator and || is the logical OR operator.

For example, we can write:

if (weight > 300 && time < 6) {
  alert("athlete");
} else {
  alert("chef");
}

to check the value of weight and time together.

If weight is less than 300 and time is less than 6, then the first block is run.

Otherwise, then 2nd block is run.

We can chain more conditional together:

if (weight > 300 && time < 6 && age > 17 && gender === "male") {
  alert("athlete");
} else {
  alert("chef");
}

This will test if all the conditional are true . If they’re all true , then the first block is run,

Otherwise, the 2nd block is run.

The || operator can be used in a similar way.

For example, we can write:

if (SAT > avg || GPA > 2.5 || sport === "football") {
  alert("accepted");
} else {
  alert("not accepted");
}

The || operator returns true if any of the conditional returns true .

We can mix || and && together.

For example, we can write:

if (age > 65 || age < 21 && res === "U.S.")

The && takes precedence, so res === “U.S.” is evaluated first.

And the || operator is evaluated next.

To make this clearer, we should surround the expressions with parentheses so everyone can read it easily:

if ((age > 65 || age < 21) && res === "U.S.")

Nested if Statements

We can nest if statements. For example, we can write:

if (c === d) {
  if (x === y) {
    g = h;
  } else if (a === b) {
    g = h;
  } else {
    v = u;
  }
} else {
  v = u;
}

We nest the inner expressions with 2 spaces to make them easy to type and readable.

We shouldn’t have too many levels of nesting since it’s hard to read.

Arrays

There’s often a need to store many values in our programs.

To make this easier, we can use arrays.

For example, instead of repeatedly declaring variables to store similar values:

let city0 = "Oslo";
let city1 = "Rome";
let city2 = "Toronto";
let city3 = "Denver";
let city4 = "Los Angeles";
let city5 = "Seattle";

We can put them all into an array by writing:

let cities = ["Oslo", "Rome", "Toronto", "Denver", "Los Angeles", "Seattle"];

Arrays are very useful for storing a list of data that are fixed size or dynamic size.

We can access an array entry by its index.

For example, we can write:

cities[2]

JavaScript can be an array with different types. For example, we can write:

let mixedArray = [1, "james", "mary", true];

The index of the first entry is 0. The same naming rules for variables apply to arrays.

Arrays can only be declared once like other variables.

Conclusion

We can test more than one condition with if statements.

They can also be nested.

Arrays are useful for storing a collection of data.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *