[TIL] 변수와 타입 🙂

김윤혜·2022년 11월 7일
0

키워드
변수, 선언, 타입, 할당, 커피, typeof, boolean, number, string, 메모리, 데이터, =, 이름, 저장, console.log, let

- 변수메모리 공간을 확보하고 데이터를 담아(저장) 이에 이름을 붙이는 것. let으로 시작.

"변수를 선언하고 값을 할당(=)한다."

- 커피는 커피콩을 갈아 어쩌구 저쩌구... 하는 내용을 저장, 함축하고 있음. 변수는 많은 내용(데이터)을 이름을 붙여 간단하게 입력할 수 있고, 구구단 예시처럼 기능(연산)은 그대로 사용하되 특정 값을 변경하여 빠르고 편리하게 출력할 수 있음. 

- 타입number, string, boolean과 같이 변수에 할당할 수 있는 것.

   number : 숫자     string : '문자열'     boolean : 부등호, true / false

(+) 위 타입들이 섞인 compound 타입(자료형)으로 배열과 객체가 있음 

(+) undefined(변수에 값이 없을 때 출력), 함수도 타입이다.

- typeof는 어떤 값의 타입을 모를 때 확인하는 용도로 쓰임. 

*크롬 개발자 도구를 이용한 출력 실습예시

1) 구구단 2단 출력

// 구구단 2단 출력하기
console.log(2 * 1) // 2
console.log(2 * 2) // 4
console.log(2 * 3) // 6
console.log(2 * 4) // 8
console.log(2 * 5) // 10
console.log(2 * 6) // 12
console.log(2 * 7) // 14
console.log(2 * 8) // 16
console.log(2 * 9) // 18

2) 변수 num을 선언하여 2단부터 9단까지 할당, 편리하게 출력가능

let num = 3;

console.log(num * 1) // 2
console.log(num * 2) // 4
console.log(num * 3) // 6
console.log(num * 4) // 8
console.log(num * 5) // 10
console.log(num * 6) // 12
console.log(num * 7) // 14
console.log(num * 8) // 16
console.log(num * 9) // 18

3) typeof 연산자를 통해 타입 확인하기

console.log(typeof 1) 
console.log(typeof '1') 
console.log(typeof (1 < 2)) 
number
string
boolean
let string = 1
console.log(typeof string)
number
profile
본질에는 일치를, 비본질에는 관용을, 이 모든 것에 사랑을

0개의 댓글