>
<
>=
<=
===
!==
if (조건) {
// 조건이 참이면 이 코드 블록이 실행
} else {
// 조건식이 거짓이면 이 코드 블록이 실행
}
true
일 경우 if 중괄호 안에 있는 코드가 실행되고 ,false
일 경우 else 중괄호 안에 있는 코드가 실행된다.
if (조건1) {
} else if (조건2) {
} else {
}
두가지 조건이 한번에 적용되는 경우
논리 연산자(Logical Operator)를 사용
논리 연산자는 우항과 좌항의 피연산자를 논리 연산한다.
부정논리 연산자의 경우, 우항의 피연산자를 논리 연산한다.
!
NOT 연산자 truthy,falsy 여부를 반전시킴
&&
논리곱(AND) 연산자
true && true // true
true && false // false
false && false // false
논리곱 연산자 &&
는 두개의 피연산자가 모두 true
로 평가될 때 true
를 반환한다.
undefined && 10 // undefined
5 && false // false
// AND 연산자는 falsy한 값을 만나면, 그값을 출력
// 둘다 truthy할 경우 , 뒤에 있는 값을 출력
||
논리합(OR) 연산자
true || true // true
true || false // true
false || false // false
undefined || 10 // 10
5 || 10 // 5
5 || console.log('실행되지 않음') // 5
// OR 연산자는 truthy한 값을 만나면 , 그값을 출력
// 둘다 falsy 할 경우 , 뒤에 있는 값을 출력
논리합 연산자||
는 두개의 피연산자 중 하나만 true
로 평가되어도 true
로 반환한다
!false // true
!(3>2) // false
!undefined // true
!'Hello' // false
'Hello'를 true로 취급(truthy)
undefined를 false로 취급(falsy)
null
undefined
0
NaN
''
false
IMMUTABLE
문자열
문자열은 기본적으로 텍스트 정보이다 '
"
를 사용하여 문자를 묶어 입력한다
str[index]
let str = "CodeStates'
console.log(str[0]); // 'C'
console.log(str[4]); // 'S'
console.log(str[10]); // undefined
// read-only
문자열 안에 모든 문자는 관련된 숫자에 상응한다
Each character has a corresponding index
Concatenating Strings
+
연산자를 사용하여 접합 할수 있다+
연산자를 쓰면, string형식으로 변환 (toString)var str1 = 'Code';
var str2 = "States";
var str3 = '1';
console.log(str1 + str2); // 'CodeStates'
console.log(str3 + 7); // '17'
length PROPERTY
.length
let str = 'Codestates';
console.log(str.length); // 10
str.indexOf(searchValue)
.indexOf()
문자열에서 주어진 인수가 나타나는 문자열 인덱스와 그 자릿수를 반환
'Blue Whale' .indexOf('Blue'); // 0
'Blue Whale' .indexOf('blue'); // -1
'Blue Whale' .indexOf('Whale'); // 5
'Blue Whale Whale' .indexOf('Whale'); // 5
'canal' .lastIndexOf('a'); //3
str.inclues(searchValue)
의 경우 string값에서 value값이 있는지 없는지 판별후 Boolean값으로 리턴str.split(seperator)
split()
let str = 'Hello from the other side';
console.log(str.split(' '));
// ['Hello', 'from', 'the', 'other', 'side']
str.substring(start, end)
substring
let str = 'abcdefghij';
console.log(str.substring(0, 3)); // 'abc'
console.log(str.substring(3, 0)); // 'abc'
console.log(str.substring(1, 4)); // 'bcd'
console.log(str.substring(-1, 4)); // 'abcd', 음수는 0으로 취급
console.log(str.substring(0, 20)); // 'abcdefghij'
텍스트의 원하는 범위를 추출하고 싶을때 사용!
str.toLowerCase()
/ str.toUpperCase()
toLowerCase()
대문자를 소문자로 리턴
toUpperCase()
소문자를 대문자로 리턴
console.log('ALPHABET' .toLowerCase()); // 'alphabet'
console.log('alphabet' .toUpperCase()); // 'ALPHABET'
str.trim()
trim()
문자열의 처음과 끝에 오는 공백을 전부 깍아내어 준다
let greeting = ' leave me alone plz ';
greeting.trim() // 'leave me alone plz'
사용자의 입력을 수용할 때 유용하고 문자열의 핵심만 뽑고 싶을 경우에 사용한다!
str.slice()
slice()
문자열의 일부를 추출하거나 잘라내서는 그 부분을 새로운 문자열로 반환해준다
let str = 'supercalifragil'
str.slice(0,5); // 'super' 0에서 5까지 추출
str.slice(5); // 'califragil' 5까지 잘라서 추출
음수일 경우 뒤에서 부터 시작
str.replace()
replace()
두 개의 인수를 받아서 교체되어야 할 값 , 그자리를 교체해서 들어가려는 값을 이용해 문자열을 바꿔준다.
let msg = "haha that is so funny!"
msg.replace('haha', 'lol')
// "lol that is so funny!"
`I counted ${3 + 4} sheep`; // "I counted 7 sheep"
템플릿 리터럴
에는 ``
백틱(back-tick) 기호를 사용한다. Math 객체
Math.floor
Math.ceil
Math.random
0과 1사이의 소숫점 사이의 숫자를 임의로 생성
Math.round()
Math.round(4.9) // 5
Math.abs()
Math.abs(-456) // 456
Math.pow()
제곱
Math.pow(2,5) // 32
2 ** 5 // 32