자바스크립트 문법(1) - 연산자와 조건·반복문

윤보라·2022년 7월 21일

자바스크립트

목록 보기
1/11

[ 1. 변수 ]

  1. 변수 선언과 데이터 할당
1) let으로 변수 선언 - 값 재할당이 가능

let name = 'bora'
console.log(name)  

→ 결과값 : 
bora

name = 'boda' // 위에서 선언했던 name이라는 변수에 boda 라는 값을 재할당
console.log(name) 

→ 결과값 : 
bora
boda


2) const로 변수 선언 - 값 재할당이 불가능

const name = 'bora'
console.log(name)

const name = 'boda'  → 오류발생(재할당 불가능하므로)

[ 2. 데이터 타입 ]

1. 기본형(원시형)
	1) 숫자 (Number) : 따옴표 붙여줄 필요 x
	2) 문자열 (String) : 따옴표를 붙여줘야함 '보라'처럼.
	3) Boolean : true/false를 할당
	4) null, undefined :
  -null은 텅텅 비어 있는 값을 의미
  -undefined은 변수를 선언만 하고 값이 할당되어 있지 않은 것
2. 객체형
- 나중에 추가 ***

[ 3. 연산자 ]

  1. 문자열 이어붙이기

1) + 를 사용하여 문자열을 이어붙여줌
	문자열과 숫자를 이어붙이면 숫자가 문자로 인식
console.log('My' + ' car') // My car를 출력
console.log('1' + 2) // 12를 출력

2) 템플릿 리터럴 (Template literals) :
	백틱(``) 을 사용하여 문자열 데이터를 표현
  
  
const shoesPrice = 200000
console.log(`이 신발의 가격은 ${shoesPrice}원입니다`) 
// console.log('이 신발의 가격은 ' + shoesPrice + '원입니다') 와 동일
// + 를 활용한 문자열 붙이기보다 간결
  

2. 산술연산자 (Numeric operators) : +, -, /, *, %, **
console.log(2 + 1) // 3을 출력
console.log(2 - 1) // 1을 출력
console.log(4 / 2) // 2를 출력
console.log(2 * 3) // 6을 출력
console.log(10 % 3) // 나머지(remainder) 연산자. 1을 출력
console.log(10 ** 2) // exponentiation. 10의 2승인 100을 출력

3. 증감연산자 (Increment and Decrement operators) : ++ , --
	증감연산자는 앞, 뒤에 사용할 때 각 결과값이 달라진다

1) 앞에 사용 (자기 자신에 먼저 재할당)
let count = 1
const preCount = ++count
console.log(`count: ${count}, preCount: ${preCount}`)

→ 결과값  : count가 먼저 자기 자신에게 1을 더해서 재할당 한 후, 이를 preCount에 할당
count: 2, preCount: 2

2) 뒤에 사용 (postCount에 할당을 먼저 한 후에 자기자신에 1을 더해서 재할당)
let count = 1
const postCount = count++
console.log(`count: ${count}, postCount: ${postCount}`)

→ 결과값  : count가 postCount에 할당을 먼저 한 후 자기자신에 1을 더해서 재할당
count: 2, postCount: 1

4. 대입연산자 (Assignment operators) : =, +=, -=
const shirtsPrice = 100000
const pantsPrice = 80000
let totalPrice = 0

totalPrice += shirtsPrice // totalPrice = totalPrice + shirtsPrice 와 동일
console.log(totalPrice)
→ 결과값  : 100000

totalPrice += pantsPrice // totalPrice = totalPrice + pantsPrice 와 동일 
console.log(totalPrice)
→ 결과값  : 180000

totalPrice -= shirtsPrice // totalPrice = totalPrice - shirtsPrice 와 동일
console.log(totalPrice) 
→ 결과값  : 80000

5. 비교연산자 (Comparison operators) : <, <=, >, >=
숫자값을 비교하는 연산자. 비교연산자를 통해서 얻는 값이 바로 boolean
( true / false )

console.log(1 < 2) // 1이 2보다 작은가? true
console.log(2 <= 2) // 2가 2보다 작거나 같은가? true
console.log(1 > 2) // 1이 2보다 큰가? false
console.log(1 >= 2) // 1이 2보다 크거나 같은가? false   

6. 논리연산자 (Logical operators) : || (or), && (and), ! (not)
	|| 는 연산 대상 중 하나만 true 여도 true 리턴
	&& 는 연산 대상이 모두 true 여야만 true 리턴
	! 는 true를 false로, flase를  true로 바꿔서 리턴

7. 일치연산자 (Equality operators) : === (==는 실수 유발가능성 때문에 거의 쓰지 않음!)
	두 값이 일치하는지를 비교 
    문자열의 경우 띄어쓰기, 대소문자까지 일치해야 true를 반환

[ 4. 조건문 ]

  1. if 구문 : if (조건) { 조건을 만족할 때 실행할 코드 }
const shoesPrice = 40000
if (shoesPrice < 50000) {
	console.log('신발을 사겠습니다.')
}

2. else, else if 구문
1) else

if (조건) { 
	조건을 만족할 때 실행할 코드 
	} else { 
	조건을 불만족할 때 실행할 코드 }

2) else if
if (조건1) { 
	조건1을 만족할 때 실행할 코드 
    } else if (조건2) { 
    조건1을 불만족하나 조건2를 만족할 때 실행할 코드 
    } else { 
    조건2마저 불만족할 때 실행할 코드 }

[ 5. 반복문 ]

  1. while 구문 : while (조건) { 조건을 만족할 때 실행할 코드 }
let temperature = 20
while (temperature < 25) {
	console.log(`${temperature}도 정도면 적당한 온도입니다.`)
	temperature++ 		// 증감연산자를 활용해서 온도를 변화시킴
}

2. for 구문 : for (begin; condition; step) { 조건을 만족할 때 실행할 코드 }
for (let temperature = 20; temperature < 25; temperature++) {
	console.log(`${temperature}도 정도면 적당한 온도입니다.`)
}

***실수로 무한루프에 빠져서 프로그램의 실행이 끝나지 않는다면 ctrl + c 를 눌러서 중단

profile
Front-End 개발자

0개의 댓글