Skip to main content

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โ€‹

ValueResult
'hello'checkmark true
''checkmark true
`template`checkmark true
String(123)checkmark true
new String('hello')cross false (boxed)
123cross false
['h', 'i']cross 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"]