trivial and outdated NPM packages

is-string trivial npm logo

Checks if a value is a string.

Weekly Downloads
28,486,691
Dependencies
32 (17 distinct)
Latest Release
a year ago
Better as a utility function
Trivial

Dependency Tree

  • is-string @1.1.1 (32)
    • call-bound @1.0.4 (29)
      • call-bind-apply-helpers @1.0.2 (2)
        • es-errors @1.3.0
        • function-bind @1.1.2
      • get-intrinsic @1.3.1 (25)
        • gopd @1.2.0
        • hasown @2.0.2 (1)
          • function-bind @1.1.2
        • es-errors @1.3.0
        • get-proto @1.0.1 (8)
          • dunder-proto @1.0.1 (5)
            • call-bind-apply-helpers @1.0.2 (2)
              • es-errors @1.3.0
              • function-bind @1.1.2
            • es-errors @1.3.0
            • gopd @1.2.0
          • es-object-atoms @1.1.1 (1)
            • es-errors @1.3.0
        • has-symbols @1.1.0
        • function-bind @1.1.2
        • async-function @1.0.0
        • es-object-atoms @1.1.1 (1)
          • es-errors @1.3.0
        • math-intrinsics @1.1.0
        • es-define-property @1.0.1
        • generator-function @2.0.1
        • call-bind-apply-helpers @1.0.2 (2)
          • es-errors @1.3.0
          • function-bind @1.1.2
        • async-generator-function @1.0.0
    • has-tostringtag @1.0.2 (1)
      • has-symbols @1.1.0

About

This dependency checks if a given value is a string.

This can be done natively using the typeof operator in JavaScript:

typeof operator

typeof "foo" === "string"; // true

That’s all you need to check if a value is a string in JavaScript.

typeof 123 === "string"; // false
typeof {} === "string"; // false
typeof [] === "string"; // false
typeof null === "string"; // false
typeof undefined === "string"; // false
typeof true === "string"; // false

The typeof operator covers about 99% of string checks.

instanceof operator

In the unlikely event that you have to deal with String objects, you can use the instanceof operator:

new String("foo") instanceof String; // true
new Object("foo") instanceof String; // true
new Boolean("foo") instanceof String; // false
new Number("foo") instanceof String; // false
new Date("foo") instanceof String; // false
new RegExp("foo") instanceof String; // false
new Error("foo") instanceof String; // false
new Function("foo") instanceof String; // false
new Array("foo") instanceof String; // false
// etc.

But really, you should not use String objects.

And that’s how you check for a string in JavaScript. No dependencies needed.