Sometimes you want to ensure a given value is actually a string. For example, you may want to confirm a name property to be a string value.
This tutorial shows you how to detect whether a given value is a string in JavaScript.
Node.js Series Overview
- String Replace All Appearances
- Remove All Whitespace From a String in JavaScript
- Generate a Random String in Node.js or JavaScript
- Remove Extra Spaces From a String in JavaScript or Node.js
- Remove Numbers From a String in JavaScript or Node.js
- Get the Part Before a Character in a String in JavaScript or Node.js
- Get the Part After a Character in a String in JavaScript or Node.js
- How to Check if a Value is a String in JavaScript or Node.js
- Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
- Check if a Value is a String in JavaScript and Node.js
- Limit and Truncate a String to a Given Length in JavaScript and Node.js
- How to Run an Asynchronous Function in Array.map()
- How to Reset and Empty an Array
- for…of vs. for…in Loops
- Clone/Copy an Array in JavaScript and Node.js
- Get an Array With Unique Values (Delete Duplicates)
- Sort an Array of Integers in JavaScript and Node.js
- Sort a Boolean Array in JavaScript, TypeScript, or Node.js
- Check If an Array Contains a Given Value in JavaScript or Node.js
- Add an Item to the Beginning of an Array in JavaScript or Node.js
- Append an Item at the End of an Array in JavaScript or Node.js
- How to Exit and Stop a for Loop in JavaScript and Node.js (Coming soon)
- Split an Array Into Smaller Array Chunks in JavaScript and Node.js
- How to Get an Index in a for…of Loop in JavaScript and Node.js (Coming soon)
- How to Exit, Stop, or Break an Array#forEach Loop in JavaScript or Node.js (Coming soon)
- Callback and Promise Support in your Node.js Modules
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
- How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript
- Detect if Value Is a Promise in Node.js and JavaScript
- Get a File’s Created Date
- Get a File’s Last Modified/Updated Date
- How to Create an Empty File
- Check If a Path or File Exists
- How to Rename a File
- Check If a Path Is a Directory (Coming soon)
- Check If a Path Is a File (Coming soon)
- Retrieve the Path to the User’s Home Directory (Coming soon)
- How to Touch a File (Coming soon)
Determine If a Given Value Is a JavaScript String
JavaScript has the typeof
operator. It returns the underlying type of a variable. This operator is useful when determining whether a given input value is a string:
/**
* Determine whether the given `input` is a string.
*
* @param {*} input
*
* @returns {Boolean}
*/
function isString (input) {
return typeof input === 'string' && Object.prototype.toString.call(input) === '[object String]'
}
isString('Hello Marcus')
// true
isString(123)
// false
I guess in 99% of all cases you’re totally fine just using the typeof input === 'string'
check.
Well, you may ask “why doing the extra Object.prototype.toString.call(input)
call” then? You know, shit can go crazy in JavaScript! Sometimes you’re tinkering with prototypes of an instance or assigning a new value to the constructor property. This extra toString
comparison is the layer ensuring you’re catching the 1% cases.
Use the @supercharge/strings Package
I’m the maintainer of the @supercharge/strings package providing convenient string utilities. It provides a convenient Str.isString(value)
method determining whether a given value is a string:
const Str = require('@supercharge/strings')
const isString = Str.isString('Hello')
// true
Str.isString(null) // false
Str.isString(1234) // false
Str.isString([1, 2, 3]) // false
// … and so on :)
Enjoy!
Mentioned Resources
- @supercharge/strings repository on GitHub