Sorting an Array
The sort()
method sorts an array alphabetically:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
The sort()
method sorts an array alphabetically:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
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.
Search an array for the item “Apple”:
const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.indexOf("Apple") + 1;
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
The length
property returns the length (size) of an array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let size = fruits.length;
The JavaScript method toString()
converts an array to a string of (comma separated) array values. Continue reading JavaScript Array Methods
An array is a special variable, which can hold more than one value:
const cars = ["Saab", "Volvo", "BMW"];
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
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 |
Number.EPSILON
is the difference between the smallest floating point number greater than 1 and 1. Continue reading JavaScript Number Properties
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 returns a number as a string. Continue reading JavaScript Number Methods
JavaScript BigInt
variables are used to store big integer values that are too big to be represented by a normal JavaScript Number
.
JavaScript integers are only accurate up to 15 digits:
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 has only one type of number. Numbers can be written with or without decimals.
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:
let x = 123e5; // 12300000
let y = 123e-5; // 0.00123
Template Strings use back-ticks (“) rather than the quotes (“”) to define a string:
let text = `Hello World!`;
Template Strings allow both single and double quotes inside a stringn : Continue reading JavaScript Template Strings
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:
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");