Programming/JavaScript (65) 썸네일형 리스트형 Arrow Functions // Arrow function const calcAge3 = birthYear => 2037 - birthYear; const age3 = calcAge3(1988); console.log(age3); const yearsUntilRetirement = (birthYear, firstName) => { const age = 2037 - birthYear; const retirement = 65 - age; // return retirement; return `${firstName} retires in ${retirement} years`; } console.log(yearsUntilRetirement(1988, 'Sunny')); // prints Sunny retires in 16 years Function Declarations vs. Expressions Function declaration은 function을 declare하기 전에 call할 수 있음 Expression은 그렇게 안됨 🙅♀️ Expression은 declare하기 전에 해당 function을 call하면 에러 뜸 e.g. Uncaught ReferenceError: Cannot access 'OOO' before initialization // Function declaration function calcAge1(birthYear) { return 2037 - birthYear; } const age1 = calcAge1(1991); // Function expression // Expression produces value // Functions are not type, but va.. Activating Strict Mode The "use strict" Directive It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables. Strict Mode는 말그대로 코드가 strict mode에서 실행되어야 함을 의미함 그럼 여기서 말하는 strict mode란? 선언되지 않은 variable을 사용하면 안됨 🙅♀️ JavaScript.. Functions function을 쓰는 이유? 같은 코드를 반복해서 사용해야 하는 상황에서 해당 코드를 function으로 만들어서 call하면 reusable해짐! 이런 코드를 dry/ clean code라고 함! function을 안쓰면? 매번 manually 한땀한땀 노가다 해줘야함.. 효율적이지 않음 🙅♀️ function logger() { console.log('My name is Sunny 🌞'); } // calling / running/ invoking function logger(); logger(); logger(); function fruitProcessor(apples, oranges) { // console.log(apples, oranges); const juice = `Juice with .. Coding Challenge #4 - tip calculator using ternary operator const billValue = 430; const tip = billValue >= 50 && billValue The Conditional (Ternary) Operator ternary: composed of three parts The ternary operator evaluates the test condition. If the condition is true , expression1 is executed. If the condition is false , expression2 is executed. https://www.programiz.com/javascript/ternary-operator JavaScript Ternary Operator (with Examples) A ternary operator can be used to replace an if..else statement in certain situations. Before you learn about t.. Statements and Expressions A JavaScript program is a sequence of statements. Each statement is an instruction for the computer to do something. vs. An expression is a bit of JavaScript code that produces a value. 1 → produces 1 "hello" → produces "hello" 5 * 10 → produces 50 num > 100 → produces either true or false isHappy ? "🙂" : "🙁" → produces an emoji [1, 2, 3].pop() → produces the number 3 https://www.joshwcomeau.com.. Coding Challenge #3 - Compare the team's average scores using if / else statements const averageScoreOfDolphins = ((96 + 108 + 89) / 3).toFixed([1]); const averageScoreOfKoalas = ((88 + 91 + 110) / 3).toFixed([1]); console.log(`The average score of Dolphis is ${averageScoreOfDolphins} and the average score of Koalas is ${averageScoreOfKoalas}`); // prints The average score of Dolphis is 97.7 and the average score of Koalas is 96.3 if (averageScoreOfDolphins > averageScoreOfKoa.. The switch Statement const day = 'monday'; switch (day) { case 'monday': // day === 'monday' console.log('월요일병 물리치쟈!'); break; case 'tuesday': console.log('화요일이다!'); break; case 'wednesday': case 'thursday': console.log('Write code examples'); break; case 'friday': console.log('오예 금요일'); break; case 'saturday': case 'sunday': console.log('주말 잘 보내세요 :)'); break; default: console.log('Not a valid day!'); } 여기서 break;를.. Truthy and Falsy Values 5 falsy values: 0, '', undefined, null, NaN console.log(Boolean(0)); // prints false console.log(Boolean(undefined)); // prints false console.log(Boolean('Jonas')); // prints true console.log(Boolean({})); // prints true console.log(Boolean('')); // prints false 0은 Falsy Value이기 때문에- else 문이 실행됨 const money = 0; if (money) { console.log("Don't spend it all"); } else { console.log("나가서 돈을 벌어오쟈");.. 이전 1 2 3 4 5 6 7 다음