[JS] 변수, 연산자, 타입

H Lee·2023년 7월 24일
0
post-thumbnail

변수

변수 선언

const : 변하지 않는 데이터 값. 재할당을 하면 에러가 발생함

let : 변하는 데이터 값

const 변수명1 = '값 1';
let 변수명2 = '값 2';

연산자

산술 연산자 : + , - , / , * , % , ** , ++ , -- ,

할당 연산자 : =

비교 연산자 : > , < , >= , <= , == , === , != , !==

논리 연산자 :

  • || (A || B) : A와 B 둘 중 하나라도 참이면 참

  • && (A && B) : A와 B 둘 다 참이여야 참

  • ! (!A) : A가 참이면 거짓, 거짓이면 참


데이터 타입

string 문자

문장, 단어, 알파벳 등 " " 안에 들어가 있는 모든 문자

number 숫자

정수, 음수, 소숫점 등 " " 안에 들어가 있지 않는 숫자.

boolean 참/거짓

true, false

array

선언할 값을 [ ] 이용하여 선언

let fruit = ['바나나', '사과', '포도'];

shift()

앞에 있는 값 제거

let fruit = ['바나나', '사과', '포도'];
fruit.shift();
console.log(fruit); // let fruit = ['사과', '포도']

pop()

마지막에 있는 값 제거

let fruit = ['바나나', '사과', '포도'];
fruit.pop();
console.log(fruit); // let fruit = ['바나나', '사과']

slice()

배열 아이템을 제거내는 역활 (시작점, 끝점)

let fruit = ['바나나', '사과', '포도'];
console.log(fruit.slice(1,2)); // ['바나나', '포도']

splice()

배열 아이템을 제거하는 역활 (시작점, 지우고 싶은 갯수)

let fruit = ['바나나', '사과', '포도'];
fruit.splice(1,1)
console.log(fruit); // ['바나나', '포도']
  • slice 와 splice : slice 는 기존의 배열에서 제거하지 않음. splice 는 기존의 배열에서 제거함

includes()

아이템이 있는지 없는지 확인

let fruit = ['바나나', '사과', '포도'];
console.log(fruit.includes('바나나')); // true
console.log(fruit.includes('수박')); // false

indexOf()

아이템의 인덱스 번호를 알려줌

let fruit = ['바나나', '사과', '포도'];
console.log(fruit.includes('바나나')); // 0

object

선언

선언할 값을 { } 이용하여 선언. { key : value } 로 표현

let banana = {cost : 3000,
             color : '노랑'};

console.log(banana) // {cost : 3000, color : '노랑'}

접근

.key 혹은 ['키'] 값으로 object의 value 에 접근할 수 있음

console.log(banana.color) // 노랑
console.log(banana["color"]) // 노랑

변경, 추가

object.key = value 값을 통하여 object에 값을 추가 할 수 있음

banana.kor = '바나나';
console.log(banana.kor) // 바나나

banana.color = '초록';
console.log(banana.color) // 초록
profile
메모

0개의 댓글