December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 1 | Determining If a Value Is a String with the typeof Operator |
In Javascript, you will often need to validate that a value is a string. For example, in the next recipe, we’ll build a function that searches for multiple words in a string. Before you pass a value to the function, you’d want to check if it’s a string. Any other data type, like a number or Boolean, would cause your function to throw an error.
Throughout this book, you will see functions of this sort repeatedly, so it’s a good place to start.
Use the typeof operator:
| | function isString(value) { |
| | return typeof value === "string"; |
| | } |
| | |
| | isString(123); // → false |
| | isString("abc"); // → true |
The typeof operator ...
Read now
Unlock full access