본문 바로가기

Programming/JavaScript

(65)
Type Conversion and Coercion Type Conversion은 manually convert함 Coercion은 자바스크립에서 자동으로 변환해줌 Type Conversion const inputYear = '1991'; console.log(inputYear + 18); // prints 199118 inputYear가 string이기 때문에 그대로 이어 붙여서 199118이 콘솔에 찍힘 참고로 Type Conversion할 때 Number 첫 글자 대문자로 해줘야 함 const inputYear = '1991'; console.log(Number(inputYear), inputYear); // prints 1991 '1991' console.log(Number(inputYear) + 9); // 2000 참고로 데이터 타입에 따라 콘..
Coding Challenge #2 - BMI Comparison using if / else statements const massMark = 78; const heightMark = 1.69; const massJohn = 92; const heightJohn = 1.95; const BMIMark = (massMark / heightMark ** 2).toFixed([1]); const BMIJohn = (massJohn / (heightJohn * heightJohn)).toFixed([1]); const markHigherBMI = BMIMark > BMIJohn; // BMIMark > BMIJohn if (markHigherBMI) { console.log(`Mark's BMI (${BMIMark}) is higher than John's (${BMIJohn})!`) } else { console.log..
Taking Decisions: if / else Statements const age = 19; const isOldEnough = age >= 18; if (isOldEnough) { console.log('Sunny can start driving license'); } // prints Sunny can start driving license const age = 15; if (age >= 18) { console.log('Sunny can start driving license'); } else { const yearsLeft = 18 - age; console.log(`Sunny is too young. Wait another ${yearsLeft} years :)`); } // prints Sunny is too young. Wait another 3 year..
Strings and Template Literals JavaScript ES6 기준 const firstName = 'Sunny'; const job = 'technical writer'; const birthYear = 1988; const year = 2022; const sunny = "'I'm " + firstName + ', a ' + (year - birthYear) + ' years old ' + job + '!'; console.log(sunny); // print 'I'm Sunny, a 34 years old technical writer! 그런데 위에서 불편한 점은 스페이스 바를 매번 manually 한칸씩 해줘야 함 똑같은 결과가 나오되 자동으로 스페이스바 넣어주려면? const sunnyNew = `I'm ${firstName}, ..
Coding Challenge #1 - BMI Comparison Tried let markWeights = 78 let markHeight = 1.69 let johnWeights = 92 let johnHeight = 1.95 let markBMI = markWeights / markHeight ** 2 let johnBMI = johnWeights / (johnHeight * johnHeight) let markHigherBMI = markBMI > johnBMI console.log("Mark's BMI is" + ' ' + markBMI); // Mark's BMI is 27.309968138370508 console.log("John's BMI is" + ' ' + johnBMI); // John's BMI is 24.194608809993426 consol..
Operator Precedence console.log(now - 1991 > now - 2019); // print true as now is 2037 여기서 하나의 궁금증이라면 어떻게 javascript에서는 -와 > 연산의 우선순위를 구분하는가? 고럼 밑에 링크를 들어가보자. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table Operator precedence - JavaScript | MDN Operator precedence determines how operators are parsed concerning each other. Operators with higher precedence become..
Basic Operators Math operators const ageJonas = 2037 - 1991; console.log(ageJonas); // print 46 const 값 여러 개 출력 원하면 , 컴마로 연달아 적어주면 됨 const ageJonas = 2037 - 1991; const ageSarah = 2037 - 2018; console.log(ageJonas, ageSarah); // print 46 19 여기서 2037을 반복해서 쓰는 것보단 const로 선언해주는 게 나음 const now = 2037 const ageJonas = now - 1991; const ageSarah = now - 2018; console.log(ageJonas, ageSarah); // print 46 19 결과는 위와 동일함..
let, const and var let vs. const let은 나중에 값을 변경할 수 있지만 const로 지정하면 값 변경 안됨 so birthYear TypeError 메시지 뜸 let age = 30; age = 31; const birthYear = 1991; birthYear = 1990; // Uncaught TypeError: Assignment to constant variable. const needs an initial value const job; // Uncaught SyntaxError: Missing initializer in const declaration const로 선언할 때는 초기값 지정안해주면 SyntaxError 발행 Q. 그럼 let vs. const 어느거 써야해? A. value가 나중에 바뀔..
Data Types (feat. 7 primitive data types) There are 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null. https://developer.mozilla.org/en-US/docs/Glossary/Primitive Primitive - MDN Web Docs Glossary: Definitions of Web-related terms | MDN In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties. There are 7 primitive data types: stri..
Values and Variables let js = "amazing"; console.log(40 + 8 + 23 - 10); console.log("Jonas"); console.log(23); let firstName = "Jonas"; console.log(firstName); console.log에 string으로 바로 Jonas를 넣든 상수에 Jonas를 저장하고 console에 log를 찍든 결과는 동일함 variable을 쓰는 이유? firstName을 여러 군데서 쓴다고 가정 매번 각기 다른데의 값을 수정해줄 필요가 없다 why? firstName에 저장된 value만 수정해주면 되니까! 자바스크립트에서도 변수명 정할 때 camel case로 씀 e.g. firstName 변수명으로 정할 때 못 쓰이는 것들 reserved ..