isString()
isString(
value):value is string
Checks if a value is a string.
Parametersโ
value: unknownโ
The value to check.
Returns: value is stringโ
true if the value is a string, false otherwise.
Sinceโ
1.0.0
Exampleโ
isString('hello'); // => true
isString(''); // => true
isString(String(123)); // => true
isString(123); // => false
isString(null); // => false
isString(new String()); // => false (String object)
How it works?โ
Type guard that checks if a value is a string.
Type Narrowingโ
Common Checksโ
| Value | Result |
|---|---|
'hello' | true |
'' | true |
`template` | true |
String(123) | true |
new String('hello') | false (boxed) |
123 | false |
['h', 'i'] | false |
Use Casesโ
Validate text input ๐โ
Ensure a value is a string before manipulation. Essential for form handling and text processing.
if (isString(input)) {
const trimmed = input.trim();
saveToDatabase(trimmed);
}
Filter strings from mixed arraysโ
Extract string values from arrays containing mixed types.
const values = ["hello", 42, "world", null, true];
const strings = values.filter(isString);
// strings: string[] = ["hello", "world"]
true
false (boxed)