모든 프로그램은 값을 담고, 이름을 붙이고, 그 값의 종류를 구분하는 것부터 시작한다.
const, 나중에 재할당할 거면 let. var는 옛날 문법이니 새 코드에서 쓰지 않는다.
const piModel = "Raspberry Pi 5"; // 다시 안 바뀜
let temperature = 52.3; // 계속 갱신됨
temperature = 54.1; // OK, let이니까 재할당 가능
const hostName = "berry";
hostName = "other"; // ❌ TypeError: Assignment to constant variable.
JS의 기본(primitive) 타입은 이 정도만 알면 충분하다.
typeof 42 // "number" (정수/실수 구분 없음)
typeof 65.5 // "number"
typeof "berry" // "string"
typeof true // "boolean"
typeof undefined // "undefined" — 값이 아예 없음
typeof null // "object" — JS의 유명한 버그성 동작(그냥 외워둔다)
typeof (() => {}) // "function"
null은 "의도적으로 비워둠", undefined는 "아직 값이 할당 안 됨". 예: 온도 센서 읽기 실패 시 null을 리턴하도록 직접 설계할 수 있다.
문자열 안에 변수를 끼워 넣을 때 + 대신 백틱( ` )을 쓴다.
const temp = 67;
console.log("온도: " + temp + "°C"); // 옛날 방식
console.log(`온도: ${temp}°C`); // 요즘 방식, 훨씬 읽기 편함
const status = temp >= 65 ? "경고" : "정상";
console.log(`[${status}] 현재 온도 ${temp}°C`);
Math.round(67.8) // 68
Math.floor(67.8) // 67
Math.max(52.3, 67.8, 40.1) // 67.8
Number("54.2") // 54.2 (문자열 → 숫자)
String(54.2) // "54.2" (숫자 → 문자열)
(54.2).toFixed(1) // "54.2" (소수점 자리수 고정, 문자열 반환)
_submissions/day01.js과제 1. 섭씨 ↔ 화씨 변환기
다음 두 함수를 작성한다.
function celsiusToFahrenheit(c) {
// TODO: 공식 F = C * 9/5 + 32
}
function fahrenheitToCelsius(f) {
// TODO: 공식 C = (F - 32) * 5/9
}
// celsiusToFahrenheit(65) → 149
// fahrenheitToCelsius(149) → 65
과제 2. 상태 메시지 만들기
const cpuTemp = 71.4; 를 선언하고, 템플릿 리터럴로 다음 형식의 문자열을 만들어 console.log로 출력한다:
"[berry] CPU 71.4°C"
과제 3. 반올림 유틸
소수점 첫째 자리까지 반올림하는 함수를 만든다.
function round1(n) {
// TODO: Math.round를 이용해서 구현 (toFixed 말고 진짜 반올림된 number를 리턴)
}
// round1(67.849) → 67.8
// round1(67.85) → 67.9
과제 4 (도전). 타입 판별기
값 하나를 받아서 "number", "string", "boolean", "other" 중 하나를 리턴하는 whatType(value) 함수를 작성한다.
cd ~/Documents/javascript-2주완성
bun _grade.ts day01