01. 변수 : 데이터 불러오기
데이터를 저장할 수 있고 데이터를 불러올 수 있습니다.
let x = 100;
let y = 200;
let z = "javascript";
let x = 100, y = 200, z = "javascript" // 일렬로 쓰나 한줄씩 쓰나 똑같음
document.write(x)
document.write(y)
document.write(z)
결과보기
02. 상수 : 데이터 불러오기
상수도 데이터를 불러올 수 있습니다.
const x = 100,
y = 200,
z = "javascript"
document.write(x);
document.write(y);
document.write(z);
결과보기
03. 배열 : 데이터 불러오기
배열의 데이터를 불러오는 방법 1
const arr = [100, 200, "javascript"]
document.write(arr[0]);
document.write(arr[1]);
document.write(arr[2]);
결과보기
04. 배열 : 데이터 불러오기 : 2차 배열
배열의 데이터를 불러오는 방법 2
const arr = [100, 200, ["javascript", "jquery"]]; //배열 in 배열(문자열 "javascript"와"jquery"로 이루어짐)
document.write(arr[0]);
document.write(arr[1]);
document.write(arr[2][0]); //3번째 값(배열)안의 1번째 값을 추출
document.write(arr[2][1]); //3번째 값(배열)안의 2번재 값 추출
결과보기
05. 배열 : 데이터 불러오기 : 갯수 구하기
배열의 데이터를 불러오는 방법 3
const arr = [100, 200, "javascript"];
document.write(arr.length); // lenght : 배열의 갯수 출력
결과보기
06. 배열 : 데이터 불러오기 : for()문
배열의 데이터를 불러오는 방법 4
const arr = [100, 200, 300, 400, 500, 600, 700 ,800 ,900];
// document.write(arr[0]);
// document.write(arr[1]);
// document.write(arr[2]);
// document.write(arr[3]);
// document.write(arr[4]);
// document.write(arr[5]);
// document.write(arr[6]);
// document.write(arr[7]);
// document.write(arr[8],"
");
for(let i = 0; i<arr.length; i++){ //for(초기값, 조건식, 증감식)_값이 많아지면 번거롭기때문에 사용
document.write(arr[i]);
};
결과보기
07. 배열 : 데이터 불러오기 : forEach()
배열의 데이터를 불러오는 방법 5
const num = [100, 200, 300, 400, 500];
// for문 이용해서 출력
for( let i = 0; i < num.length; i++){
document.write(num[i]);
}
// forEach()를 이용해서 출력
num.forEach(function(el){
document.write(el);}); //콜백함수 : 함수안에 함수, 첫번째 함수가 끝나고 그다음 함수 작동
// forEach() 3가지 인자()값
num.forEach(function(el, index, array){
document.write(el); //el=elemnet=요소값
document.write(index); //index=i
document.write(array); //array=배열
})
결과보기
100200300400500
100200300400500
100
0
100,200,300,400,500
200
1
100,200,300,400,500
300
2
100,200,300,400,500
400
3
100,200,300,400,500
500
4
100,200,300,400,500
08. 배열 : 데이터 불러오기 : for of
배열의 데이터를 불러오는 방법 6
const arr = [100, 200, 300, 400, 500];
for( let i of arr ){
document.write(i); // el값 출력
}
결과보기
100200300400500
09. 배열 : 데이터 불러오기 : for in
배열의 데이터를 불러오는 방법 6
const arr = [100, 200, 300, 400, 500];
for( let i in arr ){
document.write(i); // index값 출력
document.write(arr[i]); // el(요소)값으로 출력을 원하면 for문 응용해서 이렇게!
}
결과보기
01001200230034004500
10. 배열 : 데이터 불러오기 : map()
배열의 데이터를 불러오는 방법 7 (forEach()문과 비슷함)
const arr = [100, 200, 300, 400, 500];
//forEach문을 이용해서 값을 출력
arr.forEach(function(el){
document.write(el);
console.log(el); //console.log의 값은 관리자도구-'console'에서 확인 가능
});
arr.forEach(function(el, index, array){
document.write(el);
document.write(index);
document.write(array);
});
//map 이용
arr.map(function(el){
document.write(el);
});
arr.map(function(el, index, array){ //forEach처럼 3가지 인자 값을 넣어서 사용할 수 있음.
document.write(el);
document.write(index);
document.write(array);
}); //forEach문과 같다고 생각!
결과보기
forEach문사용_
100200300400500
1000100,200,300,400,5002001100,200,300,400,5003002100,200,300,400,5004003100,200,300,400,5005004100,200,300,400,500
map() 사용_
100200300400500
1000100,200,300,400,5002001100,200,300,400,5003002100,200,300,400,5004003100,200,300,400,5005004100,200,300,400,500
11. 배열 : 데이터 불러오기 : 펼침연산자
배열 안 데이터를 하나씩 출력함 (es10에서 나옴)
const num = [100, 200, 300, 400, 500];
document.write(...num); // 배열 안 데이터 하나씩 (펼침연산자)
결과보기
100200300400500
12. 배열 : 데이터 불러오기 : 배열구조분해할당
배열 구조를 한번 깨서 재 분배함
let a, b, c;
[a, b, c] = [100, 200, "javascript"]; //구조를 깨서 재 배분함
document.write(a);
document.write(b);
document.write(c);
결과보기
100200javascript
13. 객체 : 데이터 불러오기 : 기본
객체 데이터를 불러오는 가장 기본 방법
const obj = {
a: 100,
b: 200,
c: "javascript"
}
document.write(obj.a, obj.b, obj.c);
결과보기
100200javascript
14. 객체 : 데이터 불러오기 : Object
객체 데이터를 불러오는 방법/ 현재는 잘 안쓰임
const obj = {
a: 100,
b: 200,
c: "javascript"
}
document.write(Object.keys(obj)); //키값 불러오/기잘쓰진않음
document.write(Object.values(obj)); //값 불러오기/잘쓰진않음
document.write(Object.entries(obj)); //키,값둘다 불러오기/잘쓰진않음
결과보기
a,b,c
100,200,javascript
a,100,b,200,c,javascript
15. 객체 : 데이터 불러오기 : 변수
변수에 객체 값을 저장하여 변수이름으로 출력함
const obj = {
a: 100,
b: 200,
c: "javascript"
}
const name1 = obj.a;
const name2 = obj.b;
const name3 = obj.c;
document.write(name1);
document.write(name2);
document.write(name3);
결과보기
100200javascript
16. 객체 : 데이터 불러오기 : for in
for in을 이용해 데이터 출력
const obj = {
a: 100,
b: 200,
c: "javascript"
}
for (let key in obj) { //객체의 index값 출력
document.write(obj[key]); //index값 이용, 객체 값 출력
}
결과보기
100200javascript
17. 객체 : 데이터 불러오기 : map()
map을 이용해 데이터 출력
const obj = [
{ a: 100, b: 200, c: "javascript" }
];
obj.map((el) => {
document.write(el.a); //여기서 el만 쓰면 객체로 출력됨 ([object Object])
document.write(el.b);
document.write(el.c);
});
결과보기
100200javascript
18. 객체 : 데이터 불러오기 : hasOwnProperty()
데이터 유무 파악 후 ture나 false로 반환
const obj = {
a : 100,
b : 200,
c : "javascript"
}
document.write(obj.hasOwnProperty("a")); //데이터 있는지 없는지 확인 후 t/f 반환
document.write(obj.hasOwnProperty("b"));
document.write(obj.hasOwnProperty("c"));
document.write(obj.hasOwnProperty("d")); //없는 데이터이므로 false
document.write("a" in obj); //약식으로 표현
결과보기
truetruetruefalsetrue
19. 객체 : 데이터 불러오기 : 펼침연산자 - 복사
const obj = {
a:100,
b:200,
c:"javascript"
}
const spread = { ...obj }
document.write(spread.a)
document.write(spread.b)
document.write(spread.c)
결과보기
100200javascript
20. 객체 : 데이터 불러오기 : 펼침연산자 - 추가
const obj = {
a:100,
b:200,
c:"javascript"
}
const spread = { ...obj, d: "jquery" }
document.write(spread.d)
결과보기
jquery
21. 객체 : 데이터 불러오기 : 펼침연산자 - 결합
const objA = {
a:100,
b:200,
}
const objB = {
c: "javascript",
b: "jquery"
}
const spread = { ... objA, ...objB}
document.write(spread.a);
document.write(spread.b);
document.write(spread.c);
결과보기
100jqueryjavascript
22. 객체 : 데이터 불러오기 : 비구조화 할당
const obj = {
a: 100,
b: 200,
c: "javascript"
}
const{a, b, c} = obj //구조를 분해해서 다시 할당시킴 ∴ 변수로 선언해준뒤 obj라는 객체를 다시불러서 각각 대입?시킴
document.write(a)
document.write(b)
document.write(c)
결과보기
100200javascript
23. 객체 : 데이터 불러오기 : 비구조화 할당 : 변경시킬 수 있음
const obj = {
a: 100,
b: 200,
c: "javascript"
}
const {a:name1, b:name2, c:name3 } = obj;
document.write(a)
document.write(b)
document.write(c)
결과보기
name1name2name3