Separate arrays instead of one using for loop and push method in JavaScript
A variable can hold only singal value.
Syntax:
var <array-name> = [element0, element1, element2,... elementN];
Example:
var i = 1. var stringArray = ["one", "two", "three"]; var numericArray = [1, 2, 3, 4]; var decimalArray = [1.1, 1.2, 1.3]; var booleanArray = [true, false, false, true]; var mixedArray = [1, "two", "three", 4];
You can assign only singal literal value to i. You can not assign more than one literal values to a variable i. It will be overcome this problem, JavaScript provides an array([]).
An array is a special type of variable(var), which can store multiple values using special syntax. Each and Every value is associated with numeric index starting with index of [0].
let allNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let bigNumbers = []; let smallNumbers = []; allNumbers.forEach(function (a) { if(a >= 5) { bigNumbers.push(a); } else { smallNumbers.push(a); } }); console.log("Big: " + JSON.stringify(bigNumbers)); console.log("Small: " + JSON.stringify(smallNumbers));
Share: