10 awesome and useful JAVASCRIPT TRICKS and TIPS. Part 1
Tags: javascript, beginners, webdev, tutorial
Published: 2022-02-20 21:14:48

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.

![Javascript Convert to number code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c51znyojgdkuk8xp2otm.png)

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.
![Javascript Convert to number code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/voektysy5iesh7879mik.png)
There is a third way to convert numbers within JavaScript and that is by using the `Number()` object's constructor.
![Javascript Convert to number code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jcz42tbcgvmb3mkrowo.png)
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`.

![Javascript Convert to boolean code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aam7ev3wgkniml2s7k6x.png)

####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.
![Javascript Convert to string code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fhbvmzc6negrktb76ji.png)

##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:![Javascript String interpolation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jsvsj1ed5b45por0q2q.png)
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`.
![Javascript String interpolation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahkvenprbranvesbhim8.png)
##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.
![Javascript ternary operator code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7mo33k1s5w7c8nkqjfce.png)
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.
![Javascript && operator code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/czoyvdcqefe03y75na6y.png)
 
####Operator ||
The operator `||`, unlike the previous one, is executed only if the condition to be evaluated returns `false`, `null `or `undefined`.
 ![Javascript operator || code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jccjb64sf0fxtzv7hpnh.png)

##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`.
 ![Javascript operator nullish code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kaabhmzdg7gya1brz8w.png)
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