JAVASCRIPT
JAVASCRIPT ) .length
Hweb
2024. 7. 10. 13:12
length는 인덱스+1을 의미한다.
요소의 갯수가 아니다.
const myA = [1004, 1005, 1006];
myA[30] = 1007;
let counterN = 0;
for(let i=0; i<myA.length; i++){
if(typeof myA[i] != 'undefined'){
counterN++;
}
}
document.write(myA + "<br>");
document.write(myA.length + "<br>");
document.write("요소의 갯수 : " + counterN);
//1004,1005,1006,,,,,,,,,,,,,,,,,,,,,,,,,,,,1007
//31
//요소의 갯수 : 4
const aaa = [1,2,3];
let countV = 0;
aaa[100] = 10;
document.write(aaa.length) // 101
aaa.forEach((v) => {
countV++;
});
document.write(countV); //4
문자열에서 \(이스케이프)는 세지않는다.
s = "ABC\"D"
document.write(s.length) // 5
함수
함수에 length를 치면 파라미터의 개수를 반환한다.
하지만 default, 와 rest는 반환하지 않는다.
반응형