1 === 1 // true
1 === "1" // false
true === "true" // false
1 == "1" // true
null == undefined // true
if (조건1) {
// 조건 1이 통과할 경우
} else if (조건2) {
// 조건 1이 통과하지 않고 조건 2가 통과할 경우
} else {
// 모든 조건이 통과하지 않는 경우
}
let str = 'CodeStates';
console.log(str[0]); // 'C'
console.log(str[4]); // 'S'
console.log(str[10]); // undefined
string 타입과 다른 타입 사이에 + 연산자를 쓰면 string형식으로 강제변환 (toString)
let str1 = 'Code';
let str2 = 'States';
let str3 = '1';
console.log(str1 + str2); // 'CodeStates';
console.log(str3 + 7)' // '17'
let str = 'CodeStates';
console.log(str.length); // 10
'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 (끝에서부터 셈)
let str = 'Hello from the other side';
console.log(str.split(' '));
// ['Hello', 'from', 'the', 'other', 'side']
let str = 'abcdefghij';
console.log(str.substring(0,3)); // abc
console.log(str.substring(3,0)); // abc
console.log(str.substring(-1,4); // abcd
console.log(str.substring(0,20)); //abcdefghij
console.log('ALPHABET'.toLowerCase()); // 'alphabet'
console.log('alphabet'.toUpperCase()); // 'ALPHABET'
slice()
함수는 배열로부터 특정 범위의 복사한 값을 담고있는 새로운 배열을 만드는 데 사용begin
부터 end
까지(end
는 미포함)console.log('fivespot'.slice(0,3)); // 'fiv'
console.log('fivespot'.slice(4)); // 'spot'
slice
는 첫번째 인자가 두번째 인자보다 크면 빈 문자열을 리턴한다. 반면 substring
는 절대값의 차이만큼의 문자열을 리턴한다.console.log('fivespot'.slice(3,0)); // undefined
console.log('fivespot'.substring(3,0)); // 'fiv'
slice
인자로 음수를 쓸 경우 끝에서부터 잘라낸다 (첫번째 인자로 쓸 것인지, 아님 인자 하나만 쓸 것인지에 따라 앞부분이 남을지 뒷부분이 남을지 결정됨). 반면 substring
은 음수값을 0으로 취급한다console.log('fivespot'.slice(-4)); // 'spot'
console.log('fivespot'.slice(0,-4)); // 'five'
if (num % 2 === 0) {
return true;
} else {
return false;
}
function miniCalculator(num1, num2, operator) {
// operator 연산자 (+, -, *, /)에 따른 계산식을 리턴한다
// TODO: 여기에 코드를 작성합니다.
if (operator === '+') {
return num1 + num2;
} else if (operator === '-') {
return num1 - num2;
} else if (operator === "*") {
return num1 * num2;
} else if (operator === "/") {
return num1 / num2;
}
}
function daysInMonth(month) {
// 1,3,5,7,8,10,12월은 31일을 리턴한다
// 4,6,9,11월은 30일을 리턴한다
// 2월 28일을 리턴하며 29일은 false로 리턴한다
// TODO: 여기에 코드를 작성합니다.
if (month >= 1 && month <= 12) {
if (month === 1 || month === 3 || month === 5 || month ===7 || month === 8 || month === 10 || month ===12) {
return 31;
} else if (month === 4 || month === 6 || month === 9 || month === 11) {
return 30;
} else if (month === 2) {
return 28;
} else {
return false;
}
}
}
function daysInMonth(month) {
if (month === 2) {
return 28;
} else if (month === 4 || month === 6 || month === 9 || month === 11) {
return 30;
} else {
return 31;
}
}
function makeLastSeenMsg (name, period) {
let hour = 60;
let day = 60 * 24;
if (period >= day) {
return `${name}: ${Math.floor(period / day)}일 전에 접속함`
} else if (period >= hour) {
return `${name}: ${Math.floor(period / hour)}시간 전에 접속함`
} else {
return `${name}; ${period}분 전에 접속함`
}
}