if, else if가 아닌 if, if.
기본적인 if문은 다른 조건일시 if - else if - else이지만 if - if - if도 가능하다.
const example = (a) => {if( a === 1 ) {
return "a";
} if ( a === 2) {
return "b";
} if ( a === 3){
return "c"}
};
console.log(example(3)) // c가 출력된다.
위와 같이 else if가 아니어도 출력이 되지만 마지막 if는 조건이 들어가며 else로도 가능하다.
하지만 현재까지 대부분에서 if속에 if를 사용했기에 일반적으로 if가 여러개라면 앞 조건에 종속으로 읽기에 else if 가독성이 좋은 코드라고 할 수 있다.
${ }의 사용.
string안에 변수입력을 원할시 ${}로 감싸주어 사용이 가능하다.
function nationalPensionRemainingYearCount(age_string) {
ageDetector = (age_string) => {
const newAge = Number(age_string)
const limitAge = 65-newAge
return `앞으로 ${limitAge}년 남으셨습니다`
}
return ageDetector(age_string)
}
console.log(nationalPensionRemainingYearCount(30));
return의 이용.
함수의 작동을 위해 입력하는 return은 함수의 마무리이며 그 아래의 작동을 멈추며 작동을 정지한다.
function test(){
console.log ("hello");
console.log ("안녕")
return;
};
console.log(test()); // hello, 안녕 둘다 출력.
function test(){
console.log ("hello");
return;
console.log ("안녕") // 이 실행문은 실행되지 않는다.
}
console.log(test()); // hello만 출력.
arrary console.log를 찍어보기.
array에서 각 push, pop, shift, unshift에 따른 각 console.log.
let arr = [1, 2, 3, 4, 5];
console.log(arr); // [1, 2, 3, 4, 5] 배열 그대로 출력.
console.log(arr.push(7)); // push로 인한 lenght가 6으로 출력.
console.log(arr); // [1, 2, 3, 4, 5. 7] push로 맨 만직막에 7이 추가.
console.log(arr.pop()); // pop으로 인해 맨 뒤의 요소가 빠지기에 7이 출력.
console.log(arr); // [1, 2, 3, 4, 5]
console.log(arr.shift()); // shift는 맨 앞의 요소를 뺴주기에 맨 앞 요소 1이 출력.
console.log(arr); // [2, 3, 4, 5]
console.log(arr.unshift(2)); // unshift로 맨 앞에 2라는 요소가 추가되면서 length가 5가되어 5로 출력된다.
console.log(arr); // [2, 2, 3, 4, 5]
결과를 보면 배열에 요소를 추가시 변경된 length가 출력되고 제거시에는 제거되는 요소가 출력된다.