I wish all readers a happy new year!
I'll pick up the series I started last year and today we'll talk about type conversion. If you want to read the first episodes of this series, here is this table of contents.
In the previous post we learned about the types of data that we can find in this programming language.
For example, adding double/single quotes will interpret the content as a string type. If they are numbers as a numeric type. But sometimes, we will need to do an explicit conversion. Let's see how we can do it.
To convert a data into a String, you just need to use the
String (value) function.
There are several ways to convert data to number, including the
Number(value) function, the
parseInt(value, base) function, the
parseFloat(value) function and the unary operator
+. In this post we will see the conversion using the
Number(value) function and we will talk about the others in another occasion.
But the same does not happen with addition when mixing strings and numbers.
+ can mean three things in the programming world, two of them can be an addition operation or a concatenation operation. In a conversion to Number using the
Number(value) function, it works as concatenation if at least one of the operators is a string of numeric value.
However, this same symbol is a unary operator, which used, as its name indicates, with a single operands, and located on the left side, works as an abbreviation for the
Number(value) function, since it also converts to numbers . Let's look at an example.
There is a possibility that the result in both ways is different. In this table they shared on StackOverflow you can compare both ways.
To convert a data into a Boolean, you just need to use the
As we can see, the conversion operation is not difficult, and its rules are easy to remember.
I hope you liked this post. Don't miss the next one. See you soon!
If you want to read about other topics:
- Classification of computer languages (I)
- Software development models and methodologies (Part 1)
- Suggestion for developing web pages with HTML