원문: https://javascript.plainenglish.io/12-useful-javascript-snippets-for-everyday-problems-4f08ee1ab5e
const data = ["Messi", "35", "Football Player"];
const [name, age, job_title] = data;
console.log(name, age, job_title) // Messi 35 Football Player
const people = [
{name: "James", job_title: "Accountant"},
{name: "Dylan", job_title: "Software Engineer"},
{name: "Terry", job_title: "Lawyer"},
]
let engineer = people.find(data => data.job_title === "Software Engineer")
const reverse = (input) => {
return input.split("").reverse().join("");
}
console.log(reverse("Yul Kang")) // gnaK luY
console.log(reverse("Velog")) // goleV
let placeholder1 = "Python";
let placeholder2 = "Rust";
console.log(`I'm a ${placeholder1} developer`);
// I'm a Python developer
console.log(`I'm a ${placeholder2} developer`);
// I'm a Rust developer
// if 문
if (10 > 7) {
console.log(true);
} else {
console.log(false);
}
// 삼항연산자
10 > 7 ? console.log(true) : console.log(false);
function removeDuplicates(array) {
return [...new Set(array)];
}
const uniqueCity = removeDuplicates(["Seattle", "New York", "Seoul", "Seoul", "LA", "Seattle"]);
console.log(uniqueCity);
// ["Seattle", "New York", "Seoul", "LA"]
const randomString = "Javascript"
const newArr = [...randomString];
console.log(newArr);
// ['J', 'a', 'v', 'a', 's', 'c', 'r', 'i', 'p', 't']
window.oncontextmenu = () => {console.log("Right Click is Pressed!")}
const programming_language = {C++: 1, Java:2, Python: 3};
Object.keys(programming_language).forEach((key) => {
console.log(key);
});
// C++
// Java
// Python
Object.values(programming_language).forEach((key) => {
console.log(key);
});
// 1
// 2
// 3
// 10. Smart Data Filteration
const jobs = ["Frontend Developer", "Backend Developer", "Data Scientist", "Teacher"]
const filtered_jobs1 = jobs.filter(data => data.length < 10)
const filtered_jobs2 = jobs.filter(data => data.includes("Developer"))
console.log(filtered_jobs1) // [ 'Teacher' ]
console.log(filtered_jobs2) // [ 'Frontend Developer', 'Backend Developer' ]
const foo = null ?? 'default string';
const baz = 0 ?? 42;
console.log(foo); // default string
console.log(baz); // 0
function getRectArea(width, height) {
if (isNaN(width) || isNaN(height)) {
throw 'Parameter is not a number!';
}
}
try {
getRectArea(3, "A")
} catch (err) {
console.log(`There was an error: ${err}`)
} finally {
console.log("This code block is executed regardless of try/catch results")
}
// Output:
// There was an error: Parameter is not a number!
// This code block is executed regardless of try/catch results