gistlib
To add an element to an array in JavaScript, there are multiple ways to accomplish this task.
index.tsxlet arr = [1, 2, 3]; arr.push(4); console.log(arr); // Output: [1, 2, 3, 4] 76 chars4 lines
let arr = [1, 2, 3]; arr.push(4); console.log(arr); // Output: [1, 2, 3, 4]
index.tsxlet arr = [1, 2, 3]; arr = [...arr, 4]; console.log(arr); // Output: [1, 2, 3, 4] 82 chars4 lines
let arr = [1, 2, 3]; arr = [...arr, 4]; console.log(arr); // Output: [1, 2, 3, 4]
index.tsxlet arr = [1, 2, 3]; arr = arr.concat(4); console.log(arr); // Output: [1, 2, 3, 4] 84 chars4 lines
let arr = [1, 2, 3]; arr = arr.concat(4); console.log(arr); // Output: [1, 2, 3, 4]
gistlibby LogSnag