If

Page section: posts

The conditonal if statement is very important in JS and has many uses.

The basic syntax is:

if (num > 7) alert('too high');

Its more usual to wrap the code in curly braces and you have to if there is more than one statement to execute:

if (some condition) {
    // do this
}

You can also use else within an if block to do something if the first block turns out to be false.

if (some condition) {
    // do this
} else {
    // do that
}

Finally if checking for multiple conditions and outcomes you can use else if with another condition.

if (num > 1000 ) {
    alert('warning: too high');
} else if (num > 1500) {
    alert('Danger stop now');
}

Operators

This is often used with the equals and or operators: === and ||.

There is also not equal, *and, less than, greater than, less than or equal to and greater than or equal to:

Operator What it does
!== Does not equal (type sensitive)
!= Does not equal (type insensitive)
&& and
< less than
> greater than
<= less than or equal to
>= greater than or equal to
if (pettyCash <= 50) {
    alert("We need more cash!");
}

Nested if’s

You can also nest if statement inside one another if you want several conditions to be met. This can be clearer if there are many different conditions:

if (weather === "rain") {
    if (forecast === "bad") {
        if (money < 20) {
            alert("Do not go");
        }
    }
}

The Ternary operator

This is a short way of writing if / else statement.

In an if / else statment there are 3 parts:

  1. The condition to be met
  2. What to do if true
  3. What to do if not true

These three parts can be split with the ? and the :

the condition ? when true : when false

So this:

if (pettyCash <= 50) {
    alert("We need more cash!");
}
else {
    alert("We have enough money now");
}

can be written as

pettyCash <= 50 ? alert("We need more cash!") : alert("We have enough money now");
View contents of: /static/js/date.js

// Like topmenu.js the link to this file had to be at the bottom of the page to work

// Solution: add 'defer' to the script tag: <script src="js/date.js" defer></script>


// const modified = (document.getElementById('modified').innerHTML =
//   document.lastModified);

// This next method uses code from https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics

// With elements it only works on the first element
// window.onload = alert('loaded from JS file');

let date = document.querySelector('#modified');
let lastMod = document.lastModified;

date.textContent = 'Change this into a better format. lastModified: ' + lastMod;

// Next is just getting a simple date format

let theTime = new Date();
document.querySelector('#time6').textContent =
  theTime.getDate() + '/' + theTime.getMonth() + '/' + theTime.getFullYear();

// These are the five different paragraphs

function loadParagraphs() {
  let today = new Date();
  document.querySelector('#time2').innerHTML =
    '<b>2.</b> <code>new Date()</code>:  ' + new Date();
  document.querySelector('#time3').innerHTML =
    '<b>3.</b> <code>new Date(0)</code>: ' + new Date(0);
  document.querySelector('#time4').innerHTML =
    '<b>4.</b> <code>new Date(epoch)</code>: ' + new Date(0);
  document.querySelector('#time5').innerHTML =
    '<b>5.</b> <code>new Date(1978, 10, 14)</code>: ' + new Date(1978, 10, 14);

  document.querySelector('#time6').innerHTML =
    "<b>6.</b> <code>today.getDay() + ' ' + today.getDate()</code>: " +
    today.getDay() +
    ' ' +
    today.getDate();

  document.querySelector('#time7').innerHTML =
    '<b>7. using</b> <code>today.getFullYear()</code> :' + today.getFullYear();

  document.querySelector('#time8').innerHTML =
    '<b>8. using</b> <code>today.toDateString()</code> :' +
    today.toDateString() +
    ' ✔';

  document.querySelector('#time9').innerHTML =
    '<b>9. using</b> <code>today.toISOString()</code> :' + today.toISOString();
}



// Date table
const td = document.querySelectorAll('#method-table td');
const now = new Date();

td[1].textContent = now.getFullYear();
td[4].textContent = now.getMonth();
td[7].textContent = now.getDate();
td[10].textContent = now.getDay();
td[13].textContent = now.getSeconds();
td[16].textContent = now.getMinutes();
td[19].textContent = now.getHours();
td[22].textContent = now.getYear();

console.log(td);

Table of Contents