JavaScript is over 20 years old and over time it has grown and evolved, constantly implementing improvements. It's a multi-paradigm language as it supports functional, imperative, and event-driven programming. As if that wasn't enough, it has an API for working with text, data structures, the DOM, and more.
In this post I collect 5 tips and tricks to get the most out of Javascript.
1. JavaScript Type Conversion
There are different ways to convert any data into the data type of your choice. In the following example I will explain how to convert to numeric, boolean and string.
Convert to number
Numeric data can be of two types: integer(int) and float(float), also known as decimal. To convert any data into an integer you have several options, one of them is to use the parseInt() and parseFloat() methods, depending on whether you want to work with integers or floats.
This is the traditional way and it works very well. However, there is a faster option to achieve the same result, which is to prepend the unary operator+
to the data you want to convert.
There is a third way to convert numbers within JavaScript and that is by using the Number()
object's constructor.
Ready! With these three simple tricks you can convert a data of any type to a number.
Convert to boolean
If you want to convert any data to boolean, JavaScript offers two options. The first is to use the object's Boolean()
constructor and the second is to make use of the !!
operator, also known as the Double Bang Operator
.
Convert to string
To finish the tricks of converting one data into another, there are two options with which you can transform a number into a string. The first is with the constructor of the String()
object, and the second is to concatenate the data to an empty string.
2. String interpolation
String interpolation is concatenating text with other text or variables. Previously, multiple unary + operators were used to concatenate each desired value, resulting in code like the following:
It is quite a difficult code to write and read. Therefore, it is recommended that you avoid concatenating in this way. Currently, there is a clearer option to achieve the same result and it is through string template literals
.
3. Ternary operator
Did you know that a simple if-else
can be summed up in a single line of code? This is made possible by the ternary operator, which can be viewed as a concise if.
The code is simpler and easier to read, although you should take into account the following considerations:
A variable with the condition to evaluate is needed.
What is after the operator ?
evaluates if the condition is true.
What is after the operator :
evaluates if the condition is false, just like the else statement.
The operator : it is necessary to write it even if it is not needed, if it is not needed it is used to return a null
.
4. Short circuit operators
Continuing with the topic of if
and ternaries we have the short-circuit operators, which are &&
and ||
. These operators evaluate conditions more efficiently.
Operator &&
The &&
operator acts like a simple if
, that is, without an else
. Which means that it is executed if and only if the condition evaluates to true
, otherwise it ignores the code.
Operator ||
The operator ||
, unlike the previous one, is executed only if the condition to be evaluated returns false
, null
or undefined
.
5. Operator nullish
I know that you have just read the title of this section and you are probably thinking, what is the nullish
operator? Well, this operator is ??
and it helps to exclusively validate if the value is null
or undefined
.
This operator is not as widely used, but it is quite useful to know about its existence.
Soon the second part will be ready with another 5 tips on javascript so you can get the most out of Javascript
Top comments (0)