TypeScript में for-of लूप
अगर हमें array के elements को iterate करना है,
तो हम for-of लूप का उपयोग करते हैं।
हालाँकि TypeScript में एक nuance है - इस लूप में
element के लिए variable का type
निर्दिष्ट नहीं किया जाता है, क्योंकि TypeScript
इसे automatically calculate करने में सक्षम है:
let arr: number[] = [1, 2, 3, 4, 5];
for (let elem of arr) {
console.log(elem);
}
निम्नलिखित code को TypeScript के through rewrite करें:
let arr = [1, 2, 3, 4, 5];
let res = 0;
for (let elem of arr) {
res += elem;
}
console.log(res);