플밍정리/web

[Momentum] Clone Coding -03

silvering0 2021. 7. 30. 16:03

Boolean

: True / False (0/1)

 

const amIGirl = true;

 

이때 true 는 문자가 아니라 true 라는 값이기 때문에 따옴표 안에 넣지 않는다.

 

null 은 아무것도 없다. 즉, false 와 다르다.

null 은 의도적으로 비어있음을 표현해주기 위해 선언하는 것이고,

선언이 안되어 있다면 undefined 라고 뜬다.

const amIGirl = null;
let name;

console.log(amIFirl, name);

결과는 null undefined 라고 나온다.

 

ARRAY

 

const mon = "mon";
const tue = "tue";
const wed = "wed";
const thu = "thu";
const fri = "fri";
const sat = "sat";
const sun = "sun";

const daysOfWeek = mon + tue + wed + thu + fri + sat + sun;

console.log(daysOfWeek);

 

이렇게 하면, ..? montuewedthufrisatsun 이라는 결과가 나온다. 구별이 어렵..

그래서 더하기 보단, 배열로 묶어준다.!

 

덧셈 대신

const daysOfWeek = [mon , tue , wed , thu , fri , sat , sun];

console.log(daysOfWeek);

이렇게 하면, 리스트로 요일이 만들어 진 것을 볼 수 있다.

 

하지만, 위처럼 변수 선언을 따로 해줄 필요가 없다.

const daysOfWeek = ["mon" , "tue" , "wed" , "thu" , "fri" , "sat" , "sun"];

console.log(daysOfWeek);

그냥 이렇게 바로 변수를 넣어줘도 된다!

 

array 안에서 원하는 변수를 찾는 법

n 번째 변수?

console.log(daysOfWeek[3]);

3 자리에 원하는  n 을 넣어주면 된다. (0 부터 시작! 즉, 3 = thu)

 

array 안에 변수 추가

daysOfWeek.push("ttt");
console.log(daysOfWeek);

 

Object

: 게임을 예시로, 플레이어를 만들어 보자

 

이때, 플레이어의 속성이 여러개 일 때,

배열을 사용하면, 몇번째가 무엇을 의미하는지 파악이 어렵고, 주석을 일일이 달아주기도 어렵다.

그럼 아래와 같은 형태로 작성해 줄 수 있다.

const player = {
    name: "silvering",
    address: "KOR",
    points: 13,
}

console.log(player);
console.log(player.address);

const 는 수정을 할 수 없다.

하지만,

여기서

player.address = DE;

하고 다시 console.log 를 해보면, 주소가 바뀐다.

 

여전히 const 는 안변하지만, object 안을 바꿀 때, 추가할 때는 가능하다.

 

reference : 노마드코더

https://youtu.be/j0yrN7YMEyo

 

'플밍정리 > web' 카테고리의 다른 글

[Momentum] Clone Coding -05  (0) 2021.08.02
[Momentum] Clone Coding -04  (0) 2021.08.02
[Momentum] Clone Coding -02  (0) 2021.07.30
[Momentum] Clone Coding -01  (0) 2021.07.29
알고리즘을 공부해야 하는 이유  (0) 2021.07.28