How to get the type of a variable in JS?

JavaScript: How to get the type of a variable in JS?

Just use the typeof operator to get the type of a variable. Here is how to use it:

  1. let variable = "webdevetc";
  2. console.log(typeof variable)

string

N.B. if you do typeof on a variable that is set to null, it will tell you it is an object.

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to get the type of a variable in JS?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the JavaScript language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to add a name prefix to all routes in a group?

How to view all installed PHP modules?

301 Redirect a single URL in .htaccess to a new location

How to prevent Eloquent from adding created_at or updated_at timestamps?

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?

How to redirect your website to a new domain/location in .htaccess

How to check if a user is logged in, inside a blade file?

How do you launch the PHP interactive shell?

How to block multiple IP addresses in .htaccess

How to disable file hotlinking in Apache with .htaccess