본문 바로가기
웹코딩 배우기/· JavaScript

자바스크립트 - for of 반복

by 닐기 2023. 1. 26.

See the Pen for of by nilgi (@nilgi) on CodePen.

 

 

• for (variable of iterable) 반복 가능한 요소 반복

  - 문자 반복
    const name = "Tistory";
    let text = "";
    for (const x of name) {
        text += x;
    }


  - 배열 반복
    const name2 = ["T", "i", "s", "t", "o", "r", "y"];
    let text2 = "";
    for (const x2 of name2) {
        text2 += x2;
    }

 


  - new Set 반복
    const name3 = new Set(["T", "i", "s", "t", "o", "r", "y"]);
    let text3 = "";
    for (const x3 of name3) {
        text3 += x3;
    }

 


  - map 반복
    const pc = new Map([
        ["cpu", 250000],
        ["ram", 150000],
        ["gpu", 500000]
        ]);
    let text4 = "";
    for (const x4 of pc) {
        text4 += x4 + "<br/>";
    }