![]() ![]() ![]() 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. ES6 in Action: New String Methods - offers a powerful toolkit for working with various data types.25+ JavaScript Shorthand Coding Techniques.How to Learn JavaScript Fast: Six Simple Mind Tricks.Quick Tip: How to Convert a Number to a String in JavaScript.If you found this article useful, you may also enjoy the following: Otherwise, using these methods can make the code less readable. So, if you have a value that should contain a number but you want to be safe when converting it to a string, you can use String().Īs for string interpolation and string concatenation, they’re best used when using numbers within a string. The main difference between using String() and toString() is that String() works with undefined and null values, whereas toString() doesn’t. ![]() Although these methods can produce the same results when used with numbers, there are some cases where one approach would be better than the others. This tutorial shows you four approaches that you can use to convert a number to a string in JavaScript. Then, string concatenation is used for the rest of the expression since the first operand is "it is". The addition between a and b is performed first, which leads to the addition operation between the two variables. log ( "it is " + (a + b ) + " motorway" ) // "it is 2468 motorway" This can be solved using parentheses: const a = 2000 const b = 468 console. So, instead of an addition operation between a and b like the previous example, it becomes a string concatenation operation between the two. log ( "it is " + a + b + " motorway" ) // "it is 2000468 motorway"īecause "it is" + a is evaluated first, the + operator is used for string concatenation for the rest of the expression. ![]() However, try changing the code to the following: const a = 2000 const b = 468 console. Once a string variable or literal is reached, the operation becomes a string concatenation. Since a + b is evaluated first before reaching the string, the operation is a numerical addition rather than a string concatenation. log (a + b + " motorway" ) // "2468 motorway" When using this approach with more than one number, an unexpected result might happen.įor example: const a = 2000 const b = 468 console. log ( 10 + "" ) //"10"Ĭonvert Number to String with Concatenation by SitePoint ( CodePen.Īlthough this approach is efficient (as it requires the least amount of code), it can make the code less readable. You can convert a number to a string using the + operator.įor example: console. The second approach is string concatenation. You might also be interested to how to convert a string to a number if you’re looking to do the opposite action.Ĭonvert a Number to a String Using String Concatenation String and toString() are mostly the same but treat undefined and null variables differently. For example, using numbers as inputs to functions or APIs that expect a string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |