Ep 2: Input value As Number || 9 useful code snippets for everyday JavaScript development.

Опубликовано: 27 Январь 2025
на канале: 8020Lessons
405
5

Welcome to the second episode of this series. In this series, we will look at some of the most common JavaScript snippets which will help you with your everyday JavaScript development.

You can always use 3rd party utility libraries, but knowing these vanilla JavaScript snippets is always beneficial.

And these snippets are so easy and small, that you should always prefer them instead of a 3rd party utility library.

Ever wanted an input value as Number and not string? By default we always get input value from JS as string. Then we convert it to Number.

The purpose of the number type is that mobile browsers use this for showing the right keyboards and some browsers use this for validation purposes. For example the email type will show a keyboard with the @ and '.' on the keyboard and number will show a numeric keyboard.

But, there is a better way to get the input value as Number.
```
const numInput = document.getElementById('num-input');

let num;
// Bad: parseFloat() converts the string to a number
num = parseFloat(numInput.value);

// Good: returns a numeric value
num = numInput.valueAsNumber;
```

HTMLInputElement.valueAsNumber returns a numeric value or NaN from the input value.

Full Playlist:    • Useful code snippets for everyday Jav...  

#javascript #tutorial #js #function #jsinterview #javascriptinterview #jsforbeginners #http #https #javascriptsnippets #httpsredirection

-------------------------------------

8020 Lessons is the project to help you equip yourself 💪 with the most essential skills of the 21st century. Learn 20% skills that get you 80% results.

Become a better JavaScript developer in 10 minutes ⏱️ a week.

Concise and to the point 🎯 coding lessons.

Thank You! Master and learn JavaScript like never before: https://8020lessons.in/