Let's explore ten simple yet powerful JavaScript one-liners that can help beginner developers look like pros by making their code more efficient and elegant.
The Big Picture
Imagine you're a chef who wants to prepare a delicious meal quickly and efficiently. Instead of using many complicated steps, you use some clever tricks to get the job done faster without sacrificing taste. Similarly, these JavaScript one-liners are like those cooking tricks that make your code more concise and effective.
Core Concepts
- Array Manipulation: Handling arrays efficiently.
- String Manipulation: Working with strings in a more straightforward manner.
- Destructuring: Extracting values from arrays or objects conveniently.
- Conditional (Ternary) Operator: Simplifying if-else statements.
- Logical Operators: Using logical shortcuts for cleaner code.
- Arrow Functions: Writing shorter function syntax.
Detailed Walkthrough
Cloning an Array:
let clone = [...array];
This uses the spread operator
...
to copy all elements of an array into a new array.Converting a String to an Array:
let array = [...string];
Here, the spread operator is used to split a string into an array of characters.
Swapping Two Variables:
[a, b] = [b, a];
This leverages array destructuring to swap the values of
a
andb
in a single line.Short-Circuiting Default Values:
let value = variable || 'default';
If
variable
is falsy (likenull
,undefined
,0
,''
),value
will be set to'default'
.Quick Array Filtering:
let filteredArray = array.filter(item => condition);
This filters an array based on a condition provided in an arrow function.
Short-Circuiting Default Function Arguments:
function example(arg = 'default') { return arg; }
If
arg
is not provided, it defaults to'default'
.Converting a Number to a String:
let str = num + '';
Adding an empty string to a number converts it to a string.
Checking for an Even Number:
let isEven = num % 2 === 0;
This checks if a number is even by using the modulus operator
%
.Converting a Boolean to a Number:
let num = +bool;
Using the unary plus operator
+
convertstrue
to1
andfalse
to0
.Ternary Operator for Conditional Assignment:
let result = condition ? 'yes' : 'no';
This is a compact way to assign a value based on a condition.
Understanding Through an Example
Let's put a few of these one-liners together in a small example. Suppose you have an array of numbers, and you want to create a new array with only the even numbers, converted to strings, and provide a default value if the array is empty:
let numbers = [1, 2, 3, 4, 5];
let evenStrings = numbers.filter(num => num % 2 === 0).map(num => num + '') || ['No evens'];
In this example:
- We filter the array to get only even numbers.
- We convert these numbers to strings.
- If the resulting array is empty, we provide a default array with
'No evens'
.
Conclusion and Summary
- Array Manipulation: Use the spread operator for cloning and converting.
- String Manipulation: Split strings into arrays easily.
- Destructuring: Swap variables and extract values smoothly.
- Conditional (Ternary) Operator: Simplify if-else statements.
- Logical Operators: Use short-circuiting for default values.
- Arrow Functions: Write concise functions.
Test Your Understanding
- How would you clone an array using a one-liner?
- What is the benefit of using the ternary operator in JavaScript?
- Can you write a one-liner to check if a number is odd?
These one-liners make JavaScript coding more efficient and readable. For more details and examples, you can check the full article here.
'200===Dev Language > Javascript' 카테고리의 다른 글
Typescript Introduced (0) | 2024.06.10 |
---|---|
Javascript 기본 02 - 코드 구조, 좋은 주석 (0) | 2024.05.25 |
Javascript 기본 01 - Hello (0) | 2024.05.25 |
Javascript Promise (0) | 2024.05.25 |
Javascript 엔진 : 우리가 만든 코드를 어떻게 이해할까? (0) | 2024.05.25 |