JavaScript 기초_1

hyxoo·2023년 2월 21일
1

코드스테이츠

목록 보기
5/37
post-thumbnail

📝 [Section1_Unit5] JavaScript 기초_1

✔️ console.log

JavaScript에서는 콘솔이나 터미널에 무언가를 출력시킬 때 console.log 를 사용한다.

console.log('hello world!')

📌 타입

✔️ number

JavaScript에서는 정수(integer)실수(float)를 통틀어 number로 사용한다.

typeof 1; // 'number'
typeof -1; // 'number'
typeof 1.23; // 'number'

✔️ string

string 타입의 변수는 따옴표(’), 쌍따옴표(”), 백틱(`)으로 감싸 표현한다.

typeof 'string1'; // 'string'
typeof "string2"; // 'string'
typeof `string3`; // 'string'

📎 템플릿 리터럴 (template literal)

일반적으로 string 타입의 변수를 표현할 때에는 ", ' 를 사용하는데, string` (백틱)으로 감싸면 중간에 변수를 삽입할 수 있는 템플릿 리터럴이라고 한다. 변수를 삽입할 때는 ${} 안에 변수 이름을 넣어주면 된다.

let fruit = "orange";
console.log(`I like ${fruit}!`); // I like orange!

📎 typeof

typeof 연산자는 해당 값의 타입을 string 형태로 출력해준다.

typeof 1; // 'number'
typeof 'hello' // 'string'

📎 length

length 속성으로 문자열의 길이를 확인할 수 있다. 공백도 한칸으로 포함된다.

console.log('hello world!'.length); // 12

✔️ boolean

boolean은 사실 관계를 나타내주는 타입이며 truefalse 둘 중 하나의 값을 가진다.

📎 비교 연산자

  • ===, !== : 엄격한 비교 연산자. 같으면 true, 다르면 false.
  • ==, != : 느슨한 비교 연산자. 사용이 권장되지 않음.
  • > , < , >= , <= : 대소 관계 비교 연산자. 수학에서 부등호가 가지는 의미와 같다.

📌 변수 선언

JavaScript에서 변수를 선언하는 방법은 var, let, const 3가지가 있다.

✔️ var

varlet 이 등장하기 이전에 만들어진 방법이며 중복 선언재할당이 가능하다. 중복 선언을 사용할 경우 여러 문제점이 생길 수 있으니 주의해야 한다.

✔️ let

let 은 일반적으로 변수를 선언할 때 사용한다. 중복 선언을 하면 SyntaxError가 발생하며 재할당은 가능하다.

let fruit = 'apple';
fruit = 'orange';
let fruit = 'grape'; // SyntaxError 발생

✔️ const

const 는 변하지 않는 변수, 즉 상수를 선언할 때 사용한다. 따라서 중복 선언과 재할당이 불가능하다.

const color = 'pink';
color = 'red'; // SyntaxError 발생
const = 'blue'; // SyntaxError 발생
profile
hello world!

0개의 댓글