Array
An array stores multiple values in one single variable.
Array
<script>
let colors = ["red", "green", "blue"];
console.log(colors);
</script>
Push Array Method
push(): Adds elements to the end of an array.
let colors = ["red", "green", "blue"];
//Push Method
colors.push("yellow");
document.write(colors);
Pop Array Method
pop(): Removes the last element from an array.
let numbers = [1, 2, 3, 4, 5];
numbers.pop();
document.write(numbers);
Shift Array Method
shift(): Removes the first element from an array.
let fruits = ["apple", "banana", "orange"];
fruits.shift();
document.write(fruits);
Unshift Array Method
unshift(): Adds elements to the beginning of an array.
let animals = ["dog", "cat"];
animals.unshift("elephant", "lion");
document.write(animals);
Splice Array Method
splice(): Adds or removes elements from an array at a specified index.
let letters = ["a", "b", "c", "d", "e"];
letters.splice(2, 1, "X", "Y");
document.write(letters);
Activity 1
let Course = [“BSME”,”BSHM”,”BSBio”];
let YearLevel = [“1st year”, “2nd year”, “3rd year”, “4th year”];
let Block = [1,2,3,4,5,6];
Output:
BSME,BSIT,BSBio
BSME,BSIT,BSBio,BSCE
BMMA,BSME,BSIT,BSBio,BSCE
1st year, 2nd year, 3rd year
2,3,4,5
Concat Array Method
concat(): Concatenates two or more arrays.
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let combined = array1.concat(array2);
document.write(combined);
Join Array Method
join(): Joins all elements of an array into a string.
let words = ["Hello", "world"];
let sentence = words.join(" ");
document.write(sentence);
Slice Array Method
slice(): Extracts a section of an array and returns a new array.
let numbero = [1, 2, 3, 4, 5];
let sliced = numbero.slice(1, 4);
document.write(sliced);
indexOf Array Method
indexOf(): Returns the index of the first occurrence of a specified value
in an array.
let prutas = ["apple", "banana", "orange"];
let index = prutas.indexOf("banana");
document.write(index);
Includes Array Method
includes(): Determines whether an array includes a certain value.
let kulay = ["red", "green", "blue"];
let selectedcolor = kulay.includes("green");
document.write(selectedcolor);
Activity 2
Let Message = [“Integrative”, “Programming”, “and”,”Technologies”, 1];
let YearLevel = [“1st“, “2nd“, “3rd“, “4th“];
let Block = [1,2,3,4,5];
based on.(concat,indexOf,includes,join,slice)
Output:
1. Integrative Programming and Technologies 1
2. 1 (index of “Programming” )
3. True (if Technologies is included)
4. Programming, and, Technologies
5. 1,2,3,4,5,1st,2nd,3rd,4th