๐ด Using BackQuote
โช ` `๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด๊ณผ ๋ณ์๋ฅผ + ์ฐ์ฐ์ ์์ด ํํํ ์ ์๋ค.

const add = (a, b) => a + b;
console.log(`hello how are you ${add(6, 6)}`);
๐ HTML Fragments

const wrapper = document.querySelector(".wrapper");
const addWelcome = () => {
const div = `
<div class="hello">
<h1 class="title">Hello</h1>
</div>
`;
wrapper.innerHTML = div;
};

const wrapper = document.querySelector(".wrapper");
const friends = ["me", "lin", "hyun", "hun"];
const list = `
<h1>People i love</h1>
<ul>
${friends.map(friend => `<li>${friend}</li>`).join("")};
</ul>
`;
wrapper.innerHTML = list;
๐ก Cloning Styled Components
๋ฆฌ์กํธ์ Styled Components์ ๋น์ทํ ๋ฐฉ์์ผ๋ก ๋ฐ๋๋ผ ์๋ฐ์คํฌ๋ฆฝํธ์์ HTML ์์๋ฅผ ์ถ๊ฐํ๊ณ , CSS๋ฅผ ์ ์ฉํ ์ ์๋ ๋ฐฉ๋ฒ์ ์์๋ณด๋๋ก ํ์.

const styled = aElement => {
const el = document.createElement(aElement);
return args => {
const styles = args[0];
el.style = styles;
return el;
};
};
const title = styled("h1")`
background-color: red;
color: blue;
`;
const subtitle = styled("span")`
color: green;
`;
title.innerText = "We just cloned";
subtitle.innerText = "Styled Components";
document.body.append(title, subtitle);
๐ข Useful String Functions!
โช includes

const isEmail = email => email.includes("@");
console.log(isEmail("ryong@gmail.com")); // true
โช repeat

const CC_NUMBER = "6060";
const displayName = `${"*".repeat(10)}${CC_NUMBER}`;
console.log(displayName); // **********6060
โช startsWith && endsWith

const name = "Mr. Ryong";
console.log(name.startsWith("Mr"); // true
console.log(name.endsWith("Ryong"); // true
โช padStart && padEnd

"5".padStart(5, "x");
// xxxxx5
"5".padEnd(5, "x");
// 5xxxx
let hours = 8;
let minutes = 7;
let seconds = 28;
console.log(`${hours.padStart(2, "0").padEnd(3, "h")}:
${minutes.padStart(2, "0").padEnd(3, "m")}:
${seconds.padStart(2, "0").padEnd(3, "s")}`);
// 08h:07m:28s
โช trim, trimStart, trimEnd
โช form ์ ๋ ฅ์ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ๋ ์ ์ฉํ๋ค.

let name = " jeong seung ryong ";
name.trimStart();
// "jeong seung ryong "
name.trimEnd();
// " jeong seung ryong"
name.trim();
// "jeong seung ryong"
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ JS ] Rest And Spread (0) | 2022.07.05 |
---|---|
[ JS ] Destructuring (0) | 2022.07.05 |
[ JS ] Array (0) | 2022.07.05 |
[ JS ] Functions (0) | 2022.07.05 |
[ JS ] Variables and Operator (0) | 2022.07.05 |