CyrefJS
CCyrefJS
فا
Back to documentation
Type/Since 1.38.0

isString

Checks whether a value is a string primitive.

isString(value):value is string

Behavior

The function checks the value using typeof and returns true only when JavaScript classifies the value as the primitive string type.

Primitive Strings

String literals and values created as primitive strings return true. Objects such as new String("hello") do not return true because their typeof result is object.

Type Guard

The return type is value is string, allowing TypeScript to narrow the value to string inside a conditional branch.

Parameters

valueunknownrequired

The value to check.

Returns

value is string

Returns true when the value has the JavaScript string type; otherwise, returns false.

Examples

01

Basic usage

Check whether different values are strings.

TypeScript
import { isString } from "@cyref/js";

isString("hello");
// true

isString("");
// true

isString("123");
// true

isString(123);
// false

isString(null);
// false

isString(undefined);
// false
02

Type narrowing

isString acts as a TypeScript type guard and narrows the value to string when it returns true.

TypeScript
const value: string | number = "hello";

if (isString(value)) {
  console.log(value.toUpperCase());
}
Built with@cyref/js·Official documentation