![]() After checking its type with the typeof() operator again, you can see that it turned into a string. Then, you can pass this value to the String() function and assign the result to the same variable. Using the typeof()operator you can check the type of the value. It accepts the numeric value as a parameter and returns its string representation.įor example, let’s create a variable and assign a numeric value to it. log( typeof string) // logs "string" The String() functionĪn alternate way to convert a number into a string is by using the built-in String() function. You can go further and use an empty string in order to convert a number itself without adding any text as illustrated in the example below. const number = 7 Ĭonst string = number + "days of the week" Ĭonsole. A plus operator added a string to this variable and thus, created a new string value which you can check using a typeof() operator. In the following code there is a variable of the number type. This way you can use concatenation for converting numbers into a string. ![]() For example 5+3 will return 53 instead of 8. The process of adding one string to another using a simple plus operator will result in their joining instead of the actual mathematical operation. In JavaScript the word “concatenation” is used when talking about strings. One of the simplest ways to convert a numeric value into a string is by using concatenation which is the process of connecting one thing with another. There are several ways to transform a number into a string in JavaScript. ![]() There are situations when explicit conversion of the values is needed. For example, the alert() method will transform any passed value to a string without any additional actions by a programmer. In most cases operators and functions perform such conversions automatically. ![]() However, sometimes JS developers may face a need to convert the values from one type into another. JavaScript offers a powerful toolkit for working with various data types.
0 Comments
Leave a Reply. |