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

JavaScript Sorting Arrays

Sorting an Array

The sort() method sorts an array alphabetically:

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();

Continue reading JavaScript Sorting Arrays

JavaScript Array Search

JavaScript Array indexOf()

The indexOf() method searches an array for an element value and returns its position.

Note: The first item has position 0, the second item has position 1, and so on.

Example

Search an array for the item “Apple”:

const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.indexOf("Apple") + 1;

Syntax

array.indexOf(item, start)
item Required. The item to search for.
start Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the end.

Array.indexOf() returns -1 if the item is not found. Continue reading JavaScript Array Search

JavaScript Array Methods

JavaScript Array length

The length property returns the length (size) of an array:

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let size = fruits.length;

JavaScript Array toString()

The JavaScript method toString() converts an array to a string of (comma separated) array values. Continue reading JavaScript Array Methods