html과 javascript - 배열,반복문

정세형·2023년 2월 16일
0

javascript

목록 보기
27/30

배열 선언하는 법

 let coworkers = ["egoing", "leezche"];

배열은 0부터 시작

 document.write(coworkers[0]);
  document.write(coworkers[1]);

데이터 추가 방법

coworkers.push('duru');
coworkers.push('taeho');

배열의 길이 출력

document.write(coworkers.length);
<h1>Array</h1>
<h2>Syntax</h2>
<script>
  let coworkers = ["egoing", "leezche"];
</script>

<h2>get</h2>
<script>
  document.write(coworkers[0]);
  document.write(coworkers[1]);
</script>

<h2>add</h2>
<script>
  coworkers.push('duru');
  coworkers.push('taeho');
</script>

<h2>count</h2>
<script>
  document.write(coworkers.length);
</script>

반복문

<h1>Loop & Array</h1>
    <script>
      var coworkers = ['egoing','leezche','duru','taeho'];
    </script>
    <h2>Co workers</h2>
    <ul>
      <script>
        var i = 0;
        while(i < coworkers.length){
          document.write('<li><a href="http://a.com/'+coworkers[i]+'">'+coworkers[i]+'</a></li>');
          i = i + 1;
        }
      </script>
</ul>
profile
👨‍💻github.com/pos1504 💌pos1504@gmail.com 🙋‍♂️https://www.linkedin.com/in/%EC%84%B8%ED%98%95-%EC%A0%95-68067b287/

0개의 댓글