Another small Javascript Tidbit for enforcing a number

As I’m getting grasps with Typescript, I watched a really great Youtube tutorial video by Academind (Maximilian) that ran through tons of details on Typescript. In the video, he shows the difference between Typescript and the transpiled Javascript gen…


This content originally appeared on DEV Community and was authored by KyleFontenot

As I'm getting grasps with Typescript, I watched a really great Youtube tutorial video by Academind (Maximilian) that ran through tons of details on Typescript. In the video, he shows the difference between Typescript and the transpiled Javascript generated which shows a pretty great syntax tool.

...
const something = "5";
    > return +something
    > ...

The + character right before a variable is a way to enforce a number type! In Javascript, literal strings have become completely second-nature to most developers, and one use of it is to enforce a string by using interpolation. This + method is a really nice concise way of filtering a variable for numbers.

The + character before numbers assigns a positive values (and - for negative), so for a dynamic language like Javascript, it's assuming and enforcing the variable as a number under the hood.

This is the first time that I've seen an instance of this syntax used. Good stuff


This content originally appeared on DEV Community and was authored by KyleFontenot


Print Share Comment Cite Upload Translate Updates
APA

KyleFontenot | Sciencx (2022-06-26T18:23:18+00:00) Another small Javascript Tidbit for enforcing a number. Retrieved from https://www.scien.cx/2022/06/26/another-small-javascript-tidbit-for-enforcing-a-number/

MLA
" » Another small Javascript Tidbit for enforcing a number." KyleFontenot | Sciencx - Sunday June 26, 2022, https://www.scien.cx/2022/06/26/another-small-javascript-tidbit-for-enforcing-a-number/
HARVARD
KyleFontenot | Sciencx Sunday June 26, 2022 » Another small Javascript Tidbit for enforcing a number., viewed ,<https://www.scien.cx/2022/06/26/another-small-javascript-tidbit-for-enforcing-a-number/>
VANCOUVER
KyleFontenot | Sciencx - » Another small Javascript Tidbit for enforcing a number. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/26/another-small-javascript-tidbit-for-enforcing-a-number/
CHICAGO
" » Another small Javascript Tidbit for enforcing a number." KyleFontenot | Sciencx - Accessed . https://www.scien.cx/2022/06/26/another-small-javascript-tidbit-for-enforcing-a-number/
IEEE
" » Another small Javascript Tidbit for enforcing a number." KyleFontenot | Sciencx [Online]. Available: https://www.scien.cx/2022/06/26/another-small-javascript-tidbit-for-enforcing-a-number/. [Accessed: ]
rf:citation
» Another small Javascript Tidbit for enforcing a number | KyleFontenot | Sciencx | https://www.scien.cx/2022/06/26/another-small-javascript-tidbit-for-enforcing-a-number/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.