JavaScript is a cornerstone of modern web development, powering dynamic websites and applications. While many developers familiar with the basic and widely used features of JavaScript, numerous hidden features often go unnoticed. These lesser-known features can make your code more concise, readable, and powerful.
In this article, we’ll explore some hidden JavaScript features. From the nullish coalescing operator to Map and Set objects, each feature includes practical examples and best practices. Utilizing these features can help you write cleaner, more efficient code and tackle complex problems easily.
Whether you’re a seasoned developer or a beginner, this article will introduce you to underutilized JavaScript capabilities. By the end, you’ll have new techniques to elevate your coding skills with Javascript.
Other posts about Javascript
- JavaScript Array/Object Destructuring Explained + Examples
- The Right Way to Clone Nested Object/Array (Deep Clone) in Javascript
Lesser-known Javascript Features
1. Nullish Coalescing Operator
The nullish coalescing operator (??
) is used to provide a default value when a variable is null
or undefined
.
Code Example:
Use the nullish coalescing operator to handle cases where null
or undefined
values may appear, ensuring that your code runs smoothly with default values.
2. Optional Chaining
The optional chaining operator (?.
) allows safe access to deeply nested object properties, avoiding runtime errors if a property does not exist.
Code Example:
Use optional chaining to avoid errors when accessing properties of potentially null
or undefined
objects, making your code more robust.
3. Numeric Separators
Numeric separators (_
) make large numbers more readable by visually separating digits.
Code Example:
Use numeric separators to improve the readability of large numbers in your code, especially for financial calculations or large datasets.
4. Promise.AllSettled
Promise.allSettled
waits for all promises to settle (either fulfilled or rejected) and returns an array of objects describing the outcome.
Code Example:
Use Promise.allSettled
when you need to handle multiple promises and want to ensure that all results are processed, regardless of individual promise outcomes.
5. Private Class Fields
Private class fields are properties that can only be accessed and modified within the class they are declared.
Code Example:
Use private class fields to encapsulate data within a class, ensuring that sensitive data is not exposed or modified outside the class.
6. Logical Assignment Operators
Logical assignment operators (&&=
, ||=
, ??=
) combine logical operators with assignment, providing a concise way to update variables based on a condition.
Code Example:
Use logical assignment operators to simplify conditional assignments, making your code more readable and concise.
7. Labels for Loop and Block Statements
Labels are identifiers followed by a colon, used to label loops or blocks for reference in break or continue statements.
Code Example:
Use labels to control complex loop behavior, making it easier to manage nested loops and improve code clarity.
8. Tagged Template Literals
Tagged template literals allow you to parse template literals with a function, enabling custom processing of string literals.
Code Example 1:
Code Example 2:
Use tagged template literals for advanced string processing, such as creating safe HTML templates or localizing strings.
9. Bitwise Operators for Quick Math
Bitwise operators in JavaScript perform operations on binary representations of numbers. They are often used for low-level programming tasks, but they can also be handy for quick mathematical operations.
List of Bitwise Operators
&
(AND)|
(OR)^
(XOR)~
(NOT)<<
(Left shift)>>
(Right shift)>>>
(Unsigned right shift)
Code Example 1:
You can use the AND operator to check if a number is even or odd.
Code Example 2:
You can use left shift (<<) and right shift (>>) operators to multiply and divide by powers of 2, respectively.
Code Example 3:
You can check if a number is a power of 2 using the AND operator.
Use bitwise operators for performance-critical applications where low-level binary manipulation is required, or for quick math operations.
10. in
Operator for Property Checking
The in
operator checks if a property exists in an object.
Code Example:
Use the in
operator to verify the existence of properties in objects, ensuring that your code handles objects with missing properties gracefully.
11. debugger
Statement
The debugger
statement invokes any available debugging functionality, such as setting a breakpoint in the code.
Code Example:
Use the debugger
statement during development to pause execution and inspect code behavior, helping you identify and fix bugs more efficiently.
12. Chained Assignment
Chained assignment allows you to assign a single value to multiple variables in a single statement.
Code Example:
Use chained assignment for initializing multiple variables with the same value, reducing code redundancy.
13. Dynamic Function Names
Dynamic function names allow you to define functions with names computed at runtime.
Code Example:
Use dynamic function names to create functions with names based on runtime data, enhancing code flexibility and reusability.
14. Get Function Arguments
The arguments
object is an array-like object that contains the arguments passed to a function.
Code Example:
Use the arguments
object to access all arguments passed to a function, useful for functions with variable-length arguments.
15. Unary +
Operator
The unary operator (+
) converts its operand into a number.
Code Example:
Use the unary operator for quick type conversion, especially when working with user input or data from external sources.
16. Exponentiation **
Operator
The exponentiation operator (**
) performs exponentiation (power) of its operands.
Code Example:
Use the exponentiation operator for concise and readable mathematical expressions involving powers, such as in scientific or financial calculations.
17. Function Properties
Functions in JavaScript are objects and can have properties.
Code Example 1:
Code Example 2:
Use function properties to store metadata or configuration related to the function, enhancing the flexibility and organization of your code.
18. Object Getters & Setters
Getters and setters are methods that get or set the value of an object property.
Code Example:
Use getters and setters to encapsulate the internal state of an object, providing a controlled way to access and modify properties.
19. !!
Bang Bang Operator
The !!
(double negation) operator converts a value to its boolean equivalent.
Code Example:
Use the !!
operator to quickly convert values to booleans, useful in conditional expressions.
20. Map and Set Objects
Map
and Set
are collections with unique features. Map
holds key-value pairs, and Set
holds unique values.
Code Example 1:
Code Example 2:
Use Map
for collections of key-value pairs with any data type as keys, and Set
for collections of unique values, providing efficient ways to manage data.
Conclusion
By leveraging these lesser-known JavaScript features, you can write more efficient, readable, and robust code. Start integrating these techniques into your projects to take your JavaScript skills to the next level.
We hope this guide has provided you with valuable insights and practical examples to help you leverage these hidden JavaScript features. Don’t hesitate to experiment with them and see how they can fit into your coding practices.
If you found this article helpful, please share it with your fellow developers and friends. I’d love to hear your thoughts and experiences with these features, so feel free to leave a comment below.
Thanks. Happy coding!