Array is Collection of More Items in a given Sequence
/**
* Arrays --> Non-Primitive Data Types
*/
let studentsName = ["Sai", "Krishna", "Vijay", "Sundeep", "Sony"];
console.log(studentsName[0 ]);
console.log(studentsName[1]);
console.log(studentsName[2 ]);
console.log(studentsName[3 ]);
console.log(studentsName[4 ]);
OP: Sai
Krishna
Vijay
Sundeep
Sony
There is an problem printing Number of Times console.log so we use (DRY) Principle, Dont Repeat Yourself
Using for Loop : We can use simple line of code!
let studentsName = ["Sai", "Krishna", "Vijay", "Sundeep", "Sony"];
for (let i=0; i<6; i++) {
console.log(studentsName[i]);
}
OP: Sai
Krishna
Vijay
Sundeep
Sony
undefined
we cant give every time length so we use array length to use this as it may be any it will be printing
/**
* Arrays --> Non-Primitive Data Types
*/
let studentsName = ["Sai", "Krishna", "Vijay", "Sundeep", "Sony"];
let arrLength = studentsName.length; // Number of students printing using array length & for Condition
for (let i=0; i<arrLength; i++) {
console.log(studentsName[i]);
}
OP : It will executre all
The For Let off :
The for...of loop is a concise and elegant way to iterate over the values of an iterable object, such as an array, in JavaScript. It provides a simpler syntax compared to traditional for loops, making it a popular choice for many common array operations.
let studentsName = ["Sai", "Krishna", "Vijay", "Sundeep", "Sony"];
// let arrLength = studentsName.length; // Number of students printing using array length & for Condition
// for (let i=0; i<arrLength; i++) {
// console.log(studentsName[i]);
// }
/** for Let of */
for(let name of studentsName) {
console.log(name); // for let of is used when we no need indexing
}
OP : Sai
Krishna
Vijay
Sundeep
Sony
undefined
IMP : for let off is used when you will not use delete operations or add thse operations we can use it
For Let IN :
/** for let in */
for (let name in studentsName) {
console.log(name); //it gives the indexing as output
}
for (let index in studentsName) {
console.log(studentsName[index]); // printing the values using indexing
}
OP:
**0
1
2
3
4
Sai
Krishna
Vijay
Sundeep
Sony**