Skip to content
Fix Code Error

Check if a variable is a string in JavaScript

March 13, 2021 by Code Error
Posted By: Anonymous

How can I determine whether a variable is a string or something else in JavaScript?

Solution

You can use typeof operator:

var booleanValue = true; 
var numericalValue = 354;
var stringValue = "This is a String";
var stringObject = new String( "This is a String Object" );
alert(typeof booleanValue) // displays "boolean"
alert(typeof numericalValue) // displays "number"
alert(typeof stringValue) // displays "string"
alert(typeof stringObject) // displays "object"

Example from this webpage. (Example was slightly modified though).

This won’t work as expected in the case of strings created with new String(), but this is seldom used and recommended against[1][2]. See the other answers for how to handle these, if you so desire.


  1. The Google JavaScript Style Guide says to never use primitive object wrappers.
  2. Douglas Crockford recommended that primitive object wrappers be deprecated.
Answered By: Anonymous

Related Articles

  • What does this symbol mean in JavaScript?
  • boolean in an if statement
  • String representation of an Enum
  • Android + Pair devices via bluetooth programmatically
  • Reference — What does this symbol mean in PHP?
  • Binding complex object to a component
  • no match for ‘operator
  • Design DFA accepting binary strings divisible by a…
  • How to compare Boolean?
  • What are the undocumented features and limitations…
  • Calculator: Back key doesnt work in Javascript
  • Smart way to truncate long strings
  • The definitive guide to form-based website authentication
  • Sorting a vector of custom objects
  • Palindromic numbers in Java
  • How to filter a RecyclerView with a SearchView
  • How to set HTML5 required attribute in Javascript?
  • What is the copy-and-swap idiom?
  • You have not concluded your merge (MERGE_HEAD exists)
  • TypeScript metadata reflection references other…
  • Overloading operators in typedef structs (c++)
  • What does "Fatal error: Unexpectedly found nil while…
  • What is a NullReferenceException, and how do I fix it?
  • How to generate a random string of a fixed length in Go?
  • I am trying to subtract but it is always adding one…
  • How to implement an STL-style iterator and avoid…
  • How the int.TryParse actually works
  • Getting the closest string match
  • What is Cache-Control: private?
  • Javax.net.ssl.SSLHandshakeException:…
  • Differences in string compare methods in C#
  • PHP parse/syntax errors; and how to solve them
  • What is The Rule of Three?
  • Create a new file in git bash
  • How to detect if multiple keys are pressed at once…
  • git status shows modifications, git checkout --…
  • Difference between variable declaration syntaxes in…
  • What is an optional value in Swift?
  • Compiler error: "class, interface, or enum expected"
  • how to transform a JSON coming from an API into…
  • How to create TypeConverter which accepts multiple…
  • How to cast Object to boolean?
  • Equals(=) vs. LIKE
  • Is there some way to test my grammar which refer to…
  • Fastest way to iterate over all the chars in a String
  • Typing a redirect HOC in TypeScript with Next.js
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Good way of getting the user's location in Android
  • With Google AppScripts, Exporting data from firebase…
  • Generic function parameter with unknown args
  • How does PHP 'foreach' actually work?
  • "The system cannot find the file specified"
  • How to "properly" create a custom object in JavaScript?
  • Get the name of an object's type
  • Javascript negative number
  • Creating a "logical exclusive or" operator in Java
  • setTimeout function not working : javascript
  • Why does the arrow (->) operator in C exist?
  • Should operator
  • What are Long-Polling, Websockets, Server-Sent…
  • How do the PHP equality (== double equals) and…
  • Casting to number from query fails. Node.js Express Mongoose
  • How do I expand the output display to see more…
  • Onclick, the button moves down, why can that due to?
  • Overload operator delete to not delete object -…
  • Element implicitly has an 'any' type because…
  • Nested class: Calling child class properties in parent class
  • Detect whether a Python string is a number or a letter
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • Convert True/False value read from file to boolean
  • javascript .replace and .trim not working in vuejs
  • Error: the entity type requires a primary key
  • Declare an array in TypeScript
  • Using two values for one switch case statement
  • Json Error typeMismatch(Swift.String,…
  • Start redis-server with config file
  • Typescript Symbol.species typing inference
  • How do operator.itemgetter() and sort() work?
  • How to format a phone number in a textfield
  • How to resolve…
  • How to convert an XML file to nice pandas dataframe?
  • What are the differences between "=" and "
  • Dynamically allocating an array of objects
  • How to convert number to words in java
  • Convert array to nested JSON object - Angular Material tree
  • C++ template,typename and operator
  • Why does C++ code for testing the Collatz conjecture…
  • C - determine if a number is prime
  • Java Array, Finding Duplicates
  • Local variable referenced before assignment?
  • git add only modified changes and ignore untracked files
  • How do I check if a type is a subtype OR the type of…
  • What is a plain English explanation of "Big O" notation?
  • Where and why do I have to put the "template" and…
  • How to create ranges and synonym constants in C#?
  • SQL Server: combining multiple rows into one row
  • How to create a class specialization that does…
  • How to Update Database from Assets Folder in App
  • Graph JSON response seldom contains raw HTML
  • Conditionally displaying JSF components

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to get the user input in Java?

Next Post:

How to comment out a block of code in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error