JavaScript๋ HTML๊ณผ CSS๋ก ๋ง๋ค์ด์ง ์นํ์ด์ง๋ฅผ ๋์ ์ผ๋ก ๋ณ๊ฒฝํด์ฃผ๋ ์ธ์ด๋ค.
Variable
var (don't ever use this!) let (added in ES6)
primitive type์ ๋์ด์ ์์ ๋จ์๋ก ๋๋์ด์ง ์ ์๋ ์์ดํ ์ผ๋ก number, string, boolean, null, undefined, symbol ๋ฑ์ด ์๋ค.
object๋ ์ฑ๊ธ ์์ดํ ๋ค์ ์ฌ๋ฌ๊ฐ๋ก ๋ฌถ์ด ํ ๋จ์๋ก ๊ด๋ฆฌํ ์ ์๊ฒ ํด์ค๋ค. ์๋ฐ์คํฌ๋ฆฝํธ์์๋ function๋ ๋ฐ์ดํฐ ํ์ ์ค ํ๋๋ก์จ ๋ณ์์ ํ ๋น์ด ๊ฐ๋ฅํ๊ณ ํจ์์ ์ธ์๋ก๋ ์ ๋ฌ์ด ๋๋ค.
ํธ์ด์คํ ์ด๋ ์ด๋์ ์ ์ธํ๋์ ์๊ด์์ด ์ฝ๋๋ฅผ ์คํํ๊ธฐ ์ ๋ณ์ ์ ์ธ/ํจ์ ์ ์ธ์ด ํด๋น ์ค์ฝํ์ ์ ์ผ ์๋ก ๋์ด์ฌ๋ ค ์ฃผ๋ ๊ฒ์ด๋ค.
var testA;
console.log("test๊ฐ์" + testA); // undefinedconsole.log("test๊ฐ์" + testA); // undefined
var testA;์ต์์์ ์ ์ธํ ๊ฒ๊ณผ ๋์ผํ๋ค.
let testB;
console.log("test๊ฐ์" + testB); // undefinedconsole.log("test๊ฐ์" + testB); // Error
let testB;var๊ณผ let, const ๋ชจ๋ ํธ์ด์คํ ์ด ๋์ง๋ง ์ฐจ์ด๊ฐ ์๋ค. var์ ์ ์ธ ์ ๊ณผ ํ๊ฐ ๊ฐ๋ค. var์ ์ ์ธ๊ณผ ๋์์ ๊ฐ์ ํ ๋นํ๋ค.
let๊ณผ const์ ๊ฒฝ์ฐ var๊ณผ ๋ค๋ฅด๊ฒ ๋ณ์์ ์ ์ธ ๋จ๊ณ์ ์ด๊ธฐํ ๋จ๊ณ ์ฌ์ด์ ์ผ์์ ์ฌ๊ฐ ์ง๋(TDZ)๊ฐ ์กด์ฌํ๊ณ , TDZ์์ ๊ด๋ฆฌ ์ค์ผ ๋(let ๋ณ์์ ์ ์ธ ๋๋ const ๋ณ์์ ์ ์ธ ๋ฐ ํ ๋น ์ฝ๋๊ฐ ๋์ค๊ธฐ ์ )์ ์ฌ์ฉํ๋ ค ํ๋ค๋ฉด ReferenceError ๋ฐ์ํ๋ค.
ํธ์ด์คํ ์ ํฌ๊ฒ ํจ์ ํธ์ด์คํ ๊ณผ ๋ณ์ ํธ์ด์คํ ์ผ๋ก ๋๋๋ค.
ํจ์ ํธ์ด์คํ
ํจ์ ํธ์ด์คํ ์ ๋ฌด์๋ณด๋ค ๊ฐ์ฅ ๋จผ์ ์ด๋ฃจ์ด์ง๋ค.
๋ณ์ ํธ์ด์คํ
var์ ํธ์ด์คํ ์ด ๋ฐ์ํ๋ฉด ์ ์ธ๊ณผ ์ด๊ธฐํ๊ฐ ๊ฑฐ์ ๋์์ ์ด๋ฃจ์ด์ง๋ค. ์ ์ธ๋ถ ์์น์ ์๊ด ์์ด ์ฐธ์กฐ, ํ ๋น์ด ๊ฐ๋ฅํ๋ค.
๊ฑฐ๋ญ์ ๊ณฑ์ ๊ณฑ์ ๋ณด๋ค ์ฐ์ฐ ์ฐ์ ์์๊ฐ ๋๋ค.
๋ถ๋ฆฐํ์ด ์ซ์ํ์ผ๋ก ๋ณํ๋๋ ๊ฒฝ์ฐ true๋ 1๋ก, false๋ 0์ผ๋ก ๋ณํ๋๋ค.
๋ถ๋ฆฐํ์ผ๋ก ๋ณํํ ๋๋, ๋ณดํต true๊ฐ ๋์ค์ง๋ง, ๋น๋ฌธ์(''), 0, ๊ทธ๋ฆฌ๊ณ NaN์ false๋ก ๋ณํ๋๋ค.
// ์ผ์น ๋๋ฑ ๋น๊ต
console.log(2 === "2"); // false
console.log(2 == "2"); // true
// ๋ถ์ผ์น ๋ถ๋ฑ ๋น๊ต
console.log(2 !== "2"); // true
console.log(2 != "2"); // false๋ ๋ค "๊ฐ์ด ์๋ค"๋ ๊ฒ์ ์๋ฏธ
null : ์๋์ ์ผ๋ก "๊ฐ์ด ์๋ค"๋ ๊ฒ์ ํํํ ๋ ์ฌ์ฉ undefined : "๊ฐ์ด ์๋ค"๋ ๊ฒ์ ํ์ธํ ๋ ์ฌ์ฉ
๊ฐ์ด ์ฃผ์ด์ง์ง ์์ ๊ฐ์ ๊ธฐ๋ณธ์ ์ผ๋ก undefined
let sh;
console.log(sh); // undefinedlet sh = null;
console.log(sh); // null๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ญ์ ํ ๋๋ delete ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ค.
ํ๋กํผํฐ ๋ค์์ ๋์ด์ฐ๊ธฐ๊ฐ ์์ ๊ฒฝ์ฐ์๋ ๋๊ดํธ ํ๊ธฐ๋ฒ์ผ๋ก ์ ๊ทผํ๊ณ , ๋ฐ๋์ ๋ฐ์ดํ๋ฅผ ๋ถ์ฌ์ ๋ฌธ์์ด์ด๋ผ๋ ๊ฒ์ ๋ช ์ํด ์ฃผ์ด์ผ ํ๋ค.
let myObejct = {};
myObejct["new property"] = "new value";
console.log(myObject["new property"]);let today = new Date(2112, 7, 24);
let jaeSangStart = new Date(2109, 6, 1);
function workDayCalc(startDate) {
let timeDiff = today.getTime() - jaeSangStart.getTime();
let result = timeDiff / 1000 / 60 / 60 / 24;
console.log(`์ค๋์ ์
์ฌํ ์ง ${result + 1}์ผ์งธ ๋๋ ๋ ์
๋๋ค.`);
}
workDayCalc(jaeSangStart);arr.splice(์ญ์ ํ index, ์ญ์ ํ ๊ฐ์, ์ถ๊ฐํ ์์, ์ถ๊ฐํ ์์, ...);๋ฐฐ์ด์ ์ฒซ ์์๋ฅผ ์ญ์ : shift()
arr.shift();๋ฐฐ์ด์ ๋ง์ง๋ง ์์๋ฅผ ์ญ์ : pop()
arr.pop();๋ฐฐ์ด์ ์ฒซ ์์๋ก ๊ฐ ์ถ๊ฐ : unshift(value)
arr.unshift("value");๋ฐฐ์ด์ ๋ง์ง๋ง ์์๋ก ๊ฐ ์ถ๊ฐ : push(value)
arr.push("value");๋ฐฐ์ด์์ ํน์ ๊ฐ ์ฐพ๊ธฐ : indexOf()
let brands = ["Google", "Kakao", "Naver", "Kakao"];
console.log(brands.indexOf("Kakao")); // 1
// ํฌํจ๋์ด ์์ง ์๋ค๋ฉด -1์ด ๋ฆฌํด๋๋ค.
// ์ฌ๋ฌ๊ฐ ํฌํจ๋์ด ์๋ค๋ฉด ์ฒ์ ๋ฐ๊ฒฌ๋ ์ธ๋ฑ์ค๊ฐ ๋ฆฌํด๋๋ค.๋ฐฐ์ด์์ ๋ค์์ ๋ถํฐ ํน์ ๊ฐ ์ฐพ๊ธฐ : lastIndexOf()
let brands = ["Google", "Kakao", "Naver", "Kakao"];
console.log(brands.lastIndexOf("Kakao")); // 3๋ฐฐ์ด์์ ํน์ ๊ฐ์ด ์๋์ง ํ์ธํ๊ธฐ : includes
let brands = ["Google", "Kakao", "Naver", "Kakao"];
console.log(brands.includes("Kakao")); // true๋ฐฐ์ด ๋ค์ง๊ธฐ : reverse
let brands = ["Google", "Kakao", "Naver", "Kakao"];
brands.reverse();
console.log(brands); // ["Kakao", "Naver", "Kakao", "Google"]์ ๋๊ฐ
Math.abs(x)
์ต๋๊ฐ
console.log(Math.max(2, -1, 4, 5, 0)); // 5๋ฐ์ฌ๋ฆผ
console.log(Math.round(2.3)); // 2
// ์์์ ๋ถ๋ถ์ด 0.5 ์ด์์ด๋ฉด ๊ฐ์ฅ ๊ฐ๊น์ด ์ ์ซ๊ฐ์ผ๋ก ์ฌ๋ผ๊ฐ๊ณ , ์์์ ๋ถ๋ถ์ด 0.5 ๋ฏธ๋ง์ด๋ฉด ๊ฐ์ฅ ๊ฐ๊น์ด ์ ์ซ๊ฐ์ผ๋ก ๋ด๋ ค๊ฐ๋ค.๋ฒ๋ฆผ๊ณผ ์ฌ๋ฆผ
Math.floor(x)์ ํ๋ฉด x์ ๋ฒ๋ฆผ ๊ฐ์ด, Math.ceil(x)์ ํ๋ฉด x์ ์ฌ๋ฆผ ๊ฐ์ด ๋ฆฌํด๋๋ค. ์ด ๊ฒฝ์ฐ, ์์ ๋ถ๋ถ์ด ์ผ๋ง ์ธ์ง์๋ ์๊ด์๋ค.
console.log(Math.floor(2.49)); // 2
console.log(Math.floor(2.8)); // 2
console.log(Math.ceil(2.4)); // 3
console.log(Math.ceil(2.8)); // 3๋์
Math.random์ ํ๋ฉด 0 ์ด์ 1 ๋ฏธ๋ง์ ๊ฐ์ด ๋๋ค์ผ๋ก ๋ฆฌํด๋๋ค.
console.log(Math.random()); // 0.4235243234ํผ๋ณด๋์น ์
let previous = 0;
let current = 1;
for (let i = 1; i <= 50; i++) {
console.log(current);
let temp = previous; // previous๋ฅผ ์์ ๋ณด๊ด์ temp์ ์ ์ฅ
previous = current;
current = current + temp;
}ํฉํ ๋ฆฌ์ผ
function factorial(n) {
let result = 1;
for (let i = 1; i <= n; i++) {
result = result * i;
}
return result;
}๊ฑฐ์ค๋ฆ๋
function calculateChange(payment, cost) {
let change = payment - cost; // ๊ฑฐ์ค๋ฆ๋ ์ด์ก
const fiftyCount = Math.floor(change / 50000);
change = change - 50000 * fiftyCount;
const tenCount = Math.floor(change / 10000);
change = change - 10000 * tenCount;
const fiveCount = Math.floor(change / 5000);
change = change - 5000 * fiveCount;
const oneCount = Math.floor(change / 1000);
change = change - 1000 * oneCount;
console.log(`50000์ ์งํ: ${fiftyCount}์ฅ`);
console.log(`10000์ ์งํ: ${tenCount}์ฅ`);
console.log(`5000์ ์งํ: ${fiveCount}์ฅ`);
console.log(`1000์ ์งํ: ${oneCount}์ฅ`);
}
calculateChange(100000, 33000);
console.log("");
calculateChange(500000, 378000);ํฐ๋ฆฐ๋๋กฌ
function isPalindrome(word) {
for (let i = 0; i < Math.floor(word.length / 2); i++) {
let left = word[i];
let right = word[word.length - 1 - i];
if (left !== right) {
return false;
}
}
return true;
}
console.log(isPalindrome("racecar"));
console.log(isPalindrome("stars"));
console.log(isPalindrome("๊ธฐ๋ฌ๊ธฐ"));f