JavaScript Comparison and Logical Operators

Comparison and Logical operators are used to test for true or false.

Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values.

Given that x = 5, the table below explains the comparison operators:

Operator Description Comparing Returns Try it
== equal to x == 8 false
x == 5 true
x == “5” true
=== equal value and equal type x === 5 true
x === “5” false
!= not equal x != 8 true
!== not equal value or not equal type x !== 5 false
x !== “5” true
x !== 8 true
> greater than x > 8 false
< less than x < 8 true
>= greater than or equal to x >= 8 false
<= less than or equal to x <= 8 true

 

How Can it be Used

Comparison operators can be used in conditional statements to compare values and take action depending on the result :

if (age < 18) text = "Too young to buy alcohol";

You will learn more about the use of conditional statements in the next chapter of this tutorial. Continue reading JavaScript Comparison and Logical Operators

JavaScript Booleans

A JavaScript Boolean represents one of two values: true or false.

Boolean Values

Very often, in programming, you will need a data type that can only have one of two values, like

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, JavaScript has a Boolean data type. It can only take the values true or false. Continue reading JavaScript Booleans

JavaScript Random

Math.random()

Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive):

Example

// Returns a random number:
Math.random();

Math.random() always returns a number lower than 1.

JavaScript Random Integers

Math.random() used with Math.floor() can be used to return random integers.

There is no such thing as JavaScript integers.

We are talking about numbers with no decimals here.

Continue reading JavaScript Random

JavaScript Math Object

The JavaScript Math object allows you to perform mathematical tasks on numbers.

Example

Math.PI;

The Math Object

Unlike other objects, the Math object has no constructor.

The Math object is static.

All methods and properties can be used without creating a Math object first. Continue reading JavaScript Math Object

JavaScript Set Date Methods

Set Date methods let you set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object.

Set Date Methods

Set Date methods are used for setting a part of a date:

Method Description
setDate() Set the day as a number (1-31)
setFullYear() Set the year (yyyy)
setHours() Set the hour (0-23)
setMilliseconds() Set the milliseconds (0-999)
setMinutes() Set the minutes (0-59)
setMonth() Set the month (0-11)
setSeconds() Set the seconds (0-59)
setTime() Set the time (milliseconds since January 1, 1970)

The setFullYear() Method

The setFullYear() method sets the year of a date object. In this example to 2020 : Continue reading JavaScript Set Date Methods

JavaScript Get Date Methods

The new Date() Constructor

In JavaScript, date objects are created with new Date().

new Date() returns a date object with the current date and time.

Get the Current Time

const date = new Date();

Date Get Methods

Method Description
getFullYear() Get year as a four digit number (yyyy)
getMonth() Get month as a number (0-11)
getDate() Get day as a number (1-31)
getDay() Get weekday as a number (0-6)
getHours() Get hour (0-23)
getMinutes() Get minute (0-59)
getSeconds() Get second (0-59)
getMilliseconds() Get millisecond (0-999)
getTime() Get time (milliseconds since January 1, 1970)

Note 1

The get methods above return Local time.

Universal time (UTC) is documented at the bottom of this page.

Continue reading JavaScript Get Date Methods

JavaScript Date Formats

JavaScript Date Input

There are generally 3 types of JavaScript date input formats:

Type Example
ISO Date “2015-03-25” (The International Standard)
Short Date “03/25/2015”
Long Date “Mar 25 2015” or “25 Mar 2015”

The ISO format follows a strict standard in JavaScript.

The other formats are not so well defined and might be browser specific.

Continue reading JavaScript Date Formats

JavaScript Date Objects

JavaScript Date Objects let us work with dates:

Mon Feb 24 2025 06:31:40 GMT+0330 (Iran Standard Time)

Examples

const d = new Date();

const d = new Date("2022-03-25");

 

Note

Date objects are static. The “clock” is not “running”.

The computer clock is ticking, date objects are not.

Continue reading JavaScript Date Objects

JavaScript Array Const

ECMAScript 2015 (ES6)

In 2015, JavaScript introduced an important new keyword: const.

It has become a common practice to declare arrays using const:

Example

const cars = ["Saab", "Volvo", "BMW"];

Cannot be Reassigned

An array declared with const cannot be reassigned :

Example

const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"];    // ERROR

Arrays are Not Constants

The keyword const is a little misleading. Continue reading JavaScript Array Const

JavaScript Array Iteration

JavaScript Array forEach()

The forEach() method calls a function (a callback function) once for each array element.

Example

const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);

function myFunction(value, index, array) {
  txt += value + "<br>";
}

Note that the function takes 3 arguments:

  • The item value
  • The item index
  • The array itself

The example above uses only the value parameter. The example can be rewritten to :

Example

const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);

function myFunction(value) {
  txt += value + "<br>";
}

Continue reading JavaScript Array Iteration