목차
문자열
const firstName = 'Ara';
const job = 'teacher';
const birthYear = 1991;
const year = 2037;
const firstName = 'Ara';
const job = 'teacher';
const birthYear = 1991;
const year = 2037;
const firstName = 'Ara'; const job = 'teacher'; const birthYear = 1991; const year = 2037;
템플릿 리터럴
const araNew = `I'm ${firstName}, a ${year - birthYear} years old ${job}!`;
console.log(araNew);
// 기본 스트링
console.log(`just a regular string...`);
// 여러줄 사용
console.log('string with \n\
multiple \n\
lines');
console.log(`string with
multiple
lines`);
const araNew = `I'm ${firstName}, a ${year - birthYear} years old ${job}!`;
console.log(araNew);
// 기본 스트링
console.log(`just a regular string...`);
// 여러줄 사용
console.log('string with \n\
multiple \n\
lines');
console.log(`string with
multiple
lines`);
const araNew = `I'm ${firstName}, a ${year - birthYear} years old ${job}!`; console.log(araNew); // 기본 스트링 console.log(`just a regular string...`); // 여러줄 사용 console.log('string with \n\ multiple \n\ lines'); console.log(`string with multiple lines`);
- python의 f-string 같은 기능
- ES6부터 지원, backtick을 사용하면 됨
- 문자열만 있어도 사용할 수 있고, 멀티 라인을 써야할 때 훨씬 간결해지므로 적극적으로 사용
if / else 문
- syntax
if (condition) {
true: execute this code
} else {
false: execute this code
}
if (condition) {
true: execute this code
} else {
false: execute this code
}
if (condition) { true: execute this code } else { false: execute this code }
- e.g.
const age = 15;
if (age >= 18) {
console.log('Sarah can start driving license.');
} else {
const yearsLeft = 18 - age;
console.log(`Sarah is too young. Wait another ${yearsLeft} year :)`);
}
const age = 15;
if (age >= 18) {
console.log('Sarah can start driving license.');
} else {
const yearsLeft = 18 - age;
console.log(`Sarah is too young. Wait another ${yearsLeft} year :)`);
}
const age = 15; if (age >= 18) { console.log('Sarah can start driving license.'); } else { const yearsLeft = 18 - age; console.log(`Sarah is too young. Wait another ${yearsLeft} year :)`); }
const birthYear = 1991;
let centry;
if (birthYear <= 2000) {
centry = 20;
} else {
centry = 21;
}
const birthYear = 1991;
let centry;
if (birthYear <= 2000) {
centry = 20;
} else {
centry = 21;
}
const birthYear = 1991; let centry; if (birthYear <= 2000) { centry = 20; } else { centry = 21; }
- 코드 블록 안에서만 centry를 선언할 경우 코드 바깥에서 사용할 수 없으므로 if/else문 바깥에서
let centry;
로 변수 선언부터 해줘야 함