Arrays are used to store multiple values in a single variable.
Each value is called an element, and each element has a numeric position in the array, known as its index.
Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.
Arrays can contain any data type, including numbers, strings, and objects.
const arr1 = [2, 4, 6]; array
arr1[0]; element at index 0 → 2
arr1[1]; element at index 1 → 4
arr1[2]; element at index 2 → 6
arr1[3]; element at index 3 → undefined index not found
Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
Destructuring
The destructuring syntax is a JavaScript syntax that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
const arr2 = ["Hello", "World"]; → array
const [a, b] = arr2;
console.log(a); returns ↴
"Hello" → first element in array
console.log(b); returns ↴
"World" → second element in array
Array destructuring assigns the first and second elements of the array directly to a and b, respectively. This eliminates the need for additional indexing, making the code cleaner and more intuitive.
Temperature conversion formulas
Fahrenheit to Celsius ↴
Celsius = (Fahrenheit - 32) × 5 ⁄ 9
Fahrenheit to Kelvin ↴
Kelvin = (Fahrenheit - 32) × 5 ⁄ 9 + 273.15
Initialize a variable to hold the number representing the temperature in Fahrenheit.
const tempF = 212; → user input
Define a function convertTempF to convert temperature.
function convertTempF(degF) {}
The function takes a number as input degF and using the conversion formulas, converts it to Celsius and Kelvin.
Convert degF to Celsius and using formula ...
const degC = (degF - 32) * 5 / 9 degC
Convert degF to Kelvin using formula ...
const degK = (degF - 32) * 5 / 9 + 273.15 degK
Use destructuring to assign the returned values directly to degC and degK variables.
Call the function and destructure the results.
const [degC, degK ] = convertTempF(tempF)
degC returns → 100 Celcius value
degK returns → 373.15 Kelvin value
The results from the function may then be manipulated, for example, to display results to 2 decimal places.
Call the function with ↴
convertTempF(tempF);
Convert Fahrenheit to Celsius & Kelvin.
const tempF = 212;
function convertTempF(degF) {
const degC = (degF - 32) * 5 / 9;
const degK = (degF - 32) * 5 / 9 + 273.15;
return [degC, degK];
}
call function
const [degC, degK ] = convertTempF(tempF);
degC; returns → 100
degK; returns → 373.15
Alternative - create an array to hold the converted temperatures ↴
Convert Fahrenheit to Celsius & Kelvin.
const tempF = 212;
function convertTempF(fahrenheit) {
const arr = [];
arr[0] = (fahrenheit - 32) * 5 / 9;
arr[1] = (fahrenheit - 32) * 5 / 9 + 273.15;
return arr;
}
call function
convertTempF(tempF); returns ↴
[100, 373.15] → array contains both values
convertTempF(tempF)[0]; returns ↴
100 → first index of array [0]
convertTempF(tempF)[1]; returns ↴
373.15 → second index of array [1]