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
Kelvin to Celsius ↴
Celsius = Kelvin - 273.15
Kelvin to Fahrenheit ↴
Fahrenheit = (Kelvin - 273.15) × 9 ⁄ 5 + 32
Initialize a variable to hold the number representing the temperature in Kelvin.
const tempK = 373.15; → user input
Define a function convertTempK to convert temperature.
function convertTempK(kelvin) {}
The function takes a number as input degK and using the conversion formulas, converts it to Celsius and Fahrenheit.
Convert degK to Celsius using formula ...
const degC = degK - 273.15 degC
Convert degK to Fahrenheit using formula ...
const degF = (degK - 273.15) * 9 / 5 + 32 degF
Use destructuring to assign the returned values directly to degC and degF variables.
Call the function and destructure the results.
const [degC, degF ] = convertTempK(tempK)
degC returns → 100 Celsius value
degF returns → 212 Fahrenheit value
The results from the function may then be manipulated, for example, to display results to 2 decimal places.
Convert Kelvin to Celsius & Fahrenheit.
const tempK = 373.15;
function convertTempK(degK) {
const degC = degK - 273.15;
const degF = (degK - 273.15) * 9 / 5 + 32;
return [degC, degF];
}
call function
const [degC, degF ] = convertTempK(tempK);
degC; returns → 100
degF; returns → 212
Alternative - create an array to hold the converted temperatures ↴
Convert Kelvin to Celsius & Fahrenheit.
const tempK = 373.15;
function convertTempK(kelvin) {
const arr = [];
arr[0] = kelvin - 273.15;
arr[1] = (kelvin - 273.15) * 9 / 5 + 32;
return arr;
}
call function
convertTempK(tempK); returns ↴
[100, 212] → array contains both values
convertTempK(tempK)[0]; returns ↴
100 → first index of array [0]
convertTempK(tempK)[1]; returns ↴
212 → second index of array [1]