앱만들기

비바소·2022년 2월 12일

let num = 200
const num2= 1000
num =200
num2 =2000
console.log(num)
console.log(num2)

VM775:4 Uncaught TypeError: Assignment to constant variable.
at :4:6
(익명) @ VM775:4


error메세지 발생 이유: const num2=1000 할당된 변수에 재할당 할수없다.
const와 let 차이점이 여기에 있다.
const: 중간에 변수가 변화면 안돼는 변수라면 유용하게 쓰일수있다.

let a_list = [1,2,3,4,'giho',6,'ryu']
console.log(a_list[4])
VM899:2 giho
undefined
a_list.push('sparta') // a_list변수 list끝에 'sparta' 가 추가된다.
console.log(a_list)
VM976:2 (8) [1, 2, 3, 4, 'giho', 6, 'ryu', 'sparta']

a_list.length >>> a_list. 하면 연결연산자가 많다.
let a_dict = {"name":"giho","age":30} >>> 딕션너리
키 : 이름 , 밸류:나이
이름과 나이를 키와 밸류의 형태로
let a_dict = {"name":"giho","age":30}
console.log(a_dict["name"])

giho VM1070:2
undefined

a_dict["height"] = 200 >>> 딕션너리 추가
[" 키값 "] = 200
console.log(a_dict)
VM1144:2 {name: 'giho', age: 30, height: 200}
undefined

let names = [{"name":"gunhee", "age":"30"}][리스트{딕션너리}]형태임 리스트에 딕션너리1개 있는구조임
ex)[{"name":"gunhee", "age":"30"},
{"name":"gunhee1", "age":"30"},
{"name":"gunhee2", "age":"30"}] 3개을 리스트에 딕션너리가 있는 구조.

names.push({"name":"gunhee3", "age":"30"}) >>> 추가 할수있다.

아래와 같이 추가 될수있다...
let names = [{"name":"giho","age":30},
{"name":"giho1","age":30}]
console.log(names)
VM1332:3 (2) [{…}, {…}]
undefined
names.push({"name":"giho2","age":30}) >> 추가된 모습
3
console.log(names)
VM1342:1 (3) [{…}, {…}, {…}] >>> 3개 리스트가 되었음
undefined

names에 3개 리스트가 들어있는데 1번째 나이를 가지고 오고싶다면
names[1]["age"]
30 >>> 찍힌다

profile
한번 해보자

0개의 댓글