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

JavaScript Arrays

An array is a special variable, which can hold more than one value:

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

Why Use Arrays?

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

let car1 = "Saab";
let car2 = "Volvo";
let car3 = "BMW";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The solution is an array!

An array can hold many values under a single name, and you can access the values by referring to an index number. Continue reading JavaScript Arrays

JavaScript Number Properties

Property Description
EPSILON The difference between 1 and the smallest number > 1.
MAX_VALUE The largest number possible in JavaScript
MIN_VALUE The smallest number possible in JavaScript
MAX_SAFE_INTEGER The maximum safe integer (253 – 1)
MIN_SAFE_INTEGER The minimum safe integer -(253 – 1)
POSITIVE_INFINITY Infinity (returned on overflow)
NEGATIVE_INFINITY Negative infinity (returned on overflow)
NaN A “Not-a-Number” value

JavaScript EPSILON

Number.EPSILON is the difference between the smallest floating point number greater than 1 and 1. Continue reading JavaScript Number Properties

JavaScript Number Methods

JavaScript Number Methods

These number methods can be used on all JavaScript numbers:

Method Description
toString() Returns a number as a string
toExponential() Returns a number written in exponential notation
toFixed() Returns a number written with a number of decimals
toPrecision() Returns a number written with a specified length
valueOf() Returns a number as a number

The toString() Method

The toString() method returns a number as a string. Continue reading JavaScript Number Methods

JavaScript BigInt

JavaScript BigInt variables are used to store big integer values that are too big to be represented by a normal JavaScript Number.

JavaScript Integer Accuracy

JavaScript integers are only accurate up to 15 digits:

Integer Precision

let x = 999999999999999;
let y = 9999999999999999;

In JavaScript, all numbers are stored in a 64-bit floating-point format (IEEE 754 standard).

With this standard, large integer cannot be exactly represented and will be rounded.

Because of this, JavaScript can only safely represent integers:

Up to 9007199254740991 +(253-1)

and

Down to -9007199254740991 -(253-1).

Integer values outside this range lose precision. Continue reading JavaScript BigInt

JavaScript Numbers

JavaScript has only one type of number. Numbers can be written with or without decimals.

Example

let x = 3.14;    // A number with decimals
let y = 3;       // A number without decimals

 

Extra large or extra small numbers can be written with scientific (exponent) notation:

Example

let x = 123e5;    // 12300000
let y = 123e-5;   // 0.00123

Continue reading JavaScript Numbers

JavaScript Template Strings

Back-Tics Syntax

Template Strings use back-ticks (“) rather than the quotes (“”) to define a string:

Example

let text = `Hello World!`;

Quotes Inside Strings

Template Strings allow both single and double quotes inside a stringn : Continue reading JavaScript Template Strings

JavaScript String Search

JavaScript String indexOf()

The indexOf() method returns the index (position) of the first occurrence of a string in a string, or it returns -1 if the string is not found:

Example

let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");

Continue reading JavaScript String Search

JavaScript String Reference

JavaScript Strings

A JavaScript string stores a series of characters like “John Doe”.

A string can be any text inside double or single quotes:

let carName1 = "Volvo XC60";
let carName2 = 'Volvo XC60';

String indexes are zero-based:

The first character is in position 0, the second in 1, and so on. Continue reading JavaScript String Reference

JavaScript String Methods

JavaScript String Length

The length property returns the length of a string:

Example

let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;

Continue reading JavaScript String Methods