The differences between for loop as for...in, for...of, for await...of and forEach in Array

New Words
identical giống hệt nhau
otherwise nếu không thì
sequence sự phối hợp
sequential tuần tự
statements các câu lệnh
strictly nghiêm ngặt
traverse đi qua

for...in

Show demo below

const arr = ['a', 'b', 'c'];
for (index in arr)
{ console.log(index); }

//ouput:
//1

//2

//3

The for...in loop logs only enumerable properties of the iterable object. It doesn't log array elements a, b, c  because those are not properties — they are values. It logs array indexes as well as arr , which are actual properties. If you're not sure why these properties are iterated over, there's a more thorough explanation of how array iteration and for...in work.

Array indexes are just enumerable properties with integer names and are otherwise identical to general object properties. The for...in loop will traverse all integer keys before traversing other keys, and in strictly increasing order, making the behavior of for...in close to normal array iteration. However, the for...in loop will return all enumerable properties, including those with non–integer names and those that are inherited. Unlike for...of, for...in uses property enumeration instead of the array's iterator. In sparse arrays, for...of will visit the empty slots, but for...in will not.

It is better to use a for loop with a numeric index,  because they will return the index as a number instead of a string, and also avoid non-index properties.

for...of

Show demo below

const arr = ['a', 'b', 'c'];
for (index of arr)
{ console.log(index); }

//ouput:
// a

// b

// c

The for...of loop iterates and logs values that iterable, as an array (which is iterable), defines to be iterated over. The object's elements a, b, c are shown, but none of the object's properties are.

A for...of loop operates on the values sourced from an iterable one by one in sequential order. Each operation of the loop on a value is called an iteration, and the loop is said to iterate over the iterable. Each iteration executes statements that may refer to the current sequence value.

Difference between for...of and for...in

Both for...in and for...of statements iterate over something. The main difference between them is in what they iterate over.

The for...in statement iterates over the enumerable string properties of an object, while the for...of statement iterates over values that the iterable object defines to be iterated over.

for await...of

The for await...of statement creates a loop iterating over async iterable objects as well as on sync iterables, including: built-in String, Array, Array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined async/sync iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object. This statement can only be used inside an async function.

forEach

 The Array.prototype.forEach() is prototype method that executes a provided function once for each array element.

const arr = ['a', 'b', 'c'];
arr.forEach
{element => console.log(element); }

//ouput:
// a

// b

// c

forEach() calls a provided callbackFn function once for each element in an array in ascending index order. It is not invoked for index properties that have been deleted or are uninitialized. (For sparse arrays, see example below.)

Note: "break" and "return" can not use in forEach

const arr = ['a', 'b', 'c'];
arr.forEach
{element => {console.log(element); break;}}

//ouput:
// Uncaught SyntaxError: expected expression, got keyword 'break'

identical

@identical /ai'dentik/
* danh từ
- (ngoại giao) identic note công hàm tư tưởng (do nhiều nước cùng gửi cho một nước)
* tính từ+ (identical) /ai'dentikəl/
- đúng, chính
=the identic room where Lenin was born+ đúng căn phòng nơi Lê-nin sinh ra
- (lôgic, toán) đồng nhất
=identic proposition+ (lôgíc) mệnh đề đồng nhất

@identical
- đồng nhất

otherwise

@otherwise /'ʌðəwaiz/
* phó từ
- khác, cách khác
=he could not have acted otherwise+ anh ta đã không hành động khác được
- nếu không thì...
=seize the chance, otherwise you will regret it+ nắm lấy dịp đó, nếu không anh sẽ hối tiếc
- mặt khác, về mặt khác
=he is unruly, but not otherwise blameworthy+ nó ngang ngạnh, nhưng về mặt khác thì không đáng khiển trách

@otherwise
- cách khác, khác

sequence

@sequence /'si:kwəns/
* danh từ
- sự nối tiếp, sự liên tiếp, sự liên tục
=important events occur in rapid sequence+ những sự kiện quan trọng xảy ra liên tiếp
- cảnh (trong phim)
- (âm nhạc) phỏng chuỗi; khúc xêcăng
- (ngôn ngữ học) sự phối hợp
=sequence of tenses+ sự phối hợp các thời
- (tôn giáo) bài ca xêcăng
- (đánh bài) chuỗi quân bài cùng hoa (nối tiếp nhau)
- (toán học) dãy
=sequence of function+ dãy hàm
=sequence of number+ dãy số

@sequence
- dãy
- s. of functions dãy hàm
- s. of homomorphisms dãy đồng cấu
- s. of numbers dãy số
- arithmetic s. cấp số cộng
- Cauchy s. dãy Caoxi
- coexact s. dãy đối khớp
- cohomology s. (tô pô) dãy đối đồng đều
- completely monotonic s. dãy hoàn toàn đơn điệu
- decimal s. dãy thập phân
- double s. dãy kép
- equiconvergent s.s các dãy hội tụ đồng đều
- equivalnet s.s các dãy tương đương
- exact s. dãy khớp
- finite s. dãy hữu hạn
- homology s. dãy đồng điều
- homotopy s. dãy đồng luân
- increasing s. dãy tăng
- infinite s. dãy vô hạn
- lower s. dãy dưới
- minimizing s. (giải tích) dãy cực tiểu hoá
- moment s. dãy mômen
- monotone s. dãy đơn điệu
- monotonically increasing s. dãy tăng đơn điệu
- quasi-convex s. dãy tựa lồi
- random s. dãyngẫu nhiên
- recurrent s. (giải tích) dãy truy toán
- regular s. dãy hội tụ dãy Caoxi
- short exact s. (tô pô) dãy khớp rã
- totally monotone s. (giải tích) dãy hoàn toàn đơn điệu
- upper s. dãy trên

sequential

@sequential /'si:kwənt/ (sequential) /si'kwenʃəl/
* tính từ
- liên tục, liên tiếp
- theo sau, tiếp sau (ảnh hưởng...)
- (toán học) dãy, theo dãy
=sequent analysis+ gải tích dãy

@sequential
- (thuộc) dãy; (thống kê) liên tiếp

statements

@statement /'steitmənt/
* danh từ
- sự bày tỏ, sự trình bày, sự phát biểu
=to require clearer statement+ cần được trình bày rõ ràng hơn nữa
- lời tuyên bố; bản tuyên bố
=a well-founded statement+ lời tuyên bố có cơ sở
=joint statement+ bản tuyên bố chung

strictly

@strictly
* phó từ
- một cách nghiêm khắc; hoàn toàn

@strictly
- ngặt

traverse

@traverse /'trævə:s/
* danh từ
- sự đi ngang qua
- (toán học) đường ngang
- thanh ngang, xà ngang, đòn ngang
- (hàng hải) đường chữ chi (để tránh gió ngược...)
- đường tắt (leo núi)
=to make a traverse+ vượt núi bằng đường tắt
- (quân sự) sự xoay (súng) cho đúng hướng
- (quân sự) tường che chiến hào
- (pháp lý) sự chối
- (nghĩa bóng) sự phản đối, sự chống lại; điều làm trở ngại, điều cản trở
=it is a traverse for his plan+ đó là một điều trở ngại cho kế hoạch của hắn
* ngoại động từ
- đi qua, đi ngang qua, vượt qua; nằm vắt ngang
=to traverse a forest+ đi qua một khu rừng
=the railway traverses the country+ đường sắt chạy ngang qua vùng này
- đi (một quãng đường), đi theo (một đường phố)
- nghiên cứu kỹ lưỡng, xét kỹ toàn bộ
=to traverse a subject+ nghiên cứu toàn bộ một vấn đề
- (pháp lý) chối (trong lời biện hộ)
- (quân sự) xoay (chĩa) (súng) đúng hướng
- (ngành đường sắt) chuyển tải, sang toa
- bào (gỗ) ngang thớ
- (nghĩa bóng) phản đối, chống lại, làm thất bại (kế hoạch...)
=to traverse an opinion+ phản đối một ý kiến
* nội động từ
- xoay quanh trục (kim la bàn...)
- đi đường tắt (leo núi)
- (pháp lý) chối (trong khi biện hộ)
* tính từ
- đặt ngang, vắt ngang
!traverse sailing
- (hàng hải) sự đi theo đường chữ chi (để tránh gió ngược...)