Skip to content

Instantly share code, notes, and snippets.

@ZakiMohammed
Created February 17, 2021 18:01
Show Gist options
  • Save ZakiMohammed/ee942892eae441a9da3eb44af5eb8211 to your computer and use it in GitHub Desktop.
Save ZakiMohammed/ee942892eae441a9da3eb44af5eb8211 to your computer and use it in GitHub Desktop.
Just for fun JavaScript
// just for fun
// const names = ['John', 'Allen', 'Martin'];
const person = {
id: 1,
name: 'James',
age: 28
};
const state = 'Goa';
// forin
// --------------------------------------------
// // array
// for (const key in names) {
// console.log(`${key} - ${names[key]}`);
// }
// // string
// for (const key in state) {
// console.log(`${key} - ${state[key]}`);
// }
// // object
// for (const key in person) {
// console.log(`${key} - ${person[key]}`);
// }
const names = [];
names[5] = 'John';
names[2] = 'Allen';
for (const key in names) {
console.log(`${key} - ${names[key]}`);
}
for (const item of names) {
console.log(`${item}`);
}
names.forEach((item, index) => {
console.log(`${index} - ${item}`);
});
for (let index = 0; index < names.length; index++) {
console.log(`${index} - ${names[index]}`);
}
console.log(names);
// forof
// --------------------------------------------
// forof only works with iteratable
// // array
// for (const item of names) {
// console.log(`${item}`);
// }
// // string
// for (const item of state) {
// console.log(`${item}`);
// }
// forEach
// --------------------------------------------
// array
// names.forEach((item, index) => {
// console.log(`${index} - ${item}`);
// });
// // string
// [...state].forEach((item, index) => {
// console.log(`${index} - ${item}`);
// });
// for
// --------------------------------------------
// // array
// for (let index = 0; index < names.length; index++) {
// console.log(`${index} - ${names[index]}`);
// }
// // string
// for (let index = 0; index < state.length; index++) {
// console.log(`${index} - ${state[index]}`);
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment