Operator, if , for loop

Sshu Sshu·2022년 7월 15일
0

JS

목록 보기
5/6

1. String concatenation

'my'+'cat'
'1'+2
`string literals 1+2 = ${1+2}`
'sshu\'s \n\t book'

2. Numeric operators

add 1+1
subtract 1-1
divide 1/1
multiply 1*1
remainder 5%2
exponentiation 2**3

3. Increment and decrement operators

let counter =2;

const preIncrement = ++counter;
counter = counter +1
preIncrement = counter

const postIncrement = counter++
postIncrement = counter
counter = counter+1

preDecrement , postDecrement도 마찬가지

4. Assignment operators

let x =3;
let y =6;

x+=y (x=x+y)
x-=y
x*=y
x/=y

5. Comparison operators

10<6
106
10>6
106

6. Logical operators

  • || or → 하나라도 true면 true (true가 나온 순간 끝! 심플한 것부터 앞에 배치)
  • && and → 모두가 true면 true (false가 나온 순간 끝! )

often used to compress long if-statement

nullableObject && nullableObject.something

if(nullableObject ≠ null){
	nullableObject.something
}
  • ! not → true 를 false로 false를 true 로 변경.

7. Equality

== loose equality, with type conversion

=== strict equality, no type conversion

8. Conditional operators

if, else if, else

9. Ternary operator

condition ? value1:value2

10. Switch statement

use for multiple if checks

use for enum-like value check

use for multiple type checks in JS

switch(browser){
	case 'IE' :
		console.log('go away!')
		break;
		
	case 'Chrome':
	case 'Firefox':
		console.log('I love you!')
		break;
	
	default:
		console.log('same all!')
		break;
}

11. Loops

  • while loop 조건이 맞을때만 실행
let i =3;

while(i>0){
	console.log(`${i}`)
	i— 
}
  • do while loop 블럭을 실행한 이후 조건을 검사
do{
	console.log(`${i}`)
	i—
}while(i>0)
  • for loop
for(let i=3;i>0;i—){
	console.log(`${i}`)
}

nested loops (안좋음)

for(){
	for(){}
}

break 코드 중단

continue 이번코드 중단 후 다음 코드 실행

profile
Front-End Developer

0개의 댓글