Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to get the native type of a value in JavaScript ?
Next article icon

How to check the type of a variable or object in JavaScript ?

Last Updated : 31 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type of value. The type of the value assigned to a variable determines the type of the variable.

The typeof operator in JavaScript allows you to determine the type of value or type of value that a variable contains . There is just one operand for the typeof operator (a unary operator), which takes one variable as input. It determines the operand's type and a string is returned as a result.

Let's understand the typeof operator using some examples:

Example 1: If a string variable is checked by typeof, the result will be "string".

JavaScript
<script>   var apple = "apple";   console.log(typeof apple); </script> 

Output:

"string"

Example 2: When we check the type of a number variable it results in the string "number".

JavaScript
<script>   var number = 2052021;   console.log(typeof number); </script> 

Output:

"number"

Example 3: When we check the type of undefined it is "undefined".

JavaScript
<script>   console.log(typeof undefined); </script> 

Output:

"undefined"

Example 4: The type of special symbol is "string".

HTML
<script>   var symbol = "@";   console.log(typeof symbol); </script> 

Output:

"string"

Example 5: The type of null is "object".

HTML
<script>   console.log(typeof null); </script> 

Output:

"object"

Example 6: The type of NaN ( not a number ) returns "number".

JavaScript
<script>   console.log(typeof NaN); </script> 

Output:

"number"

Example 7: In this example, a new object is created using a new keyword. typeof the variable created as "object".

JavaScript
<script>   let obj = new String("this is a string")   console.log(typeof obj); </script> 

Output:

"object"

Example 8: In the below example, a function is created to find the sum of two numbers and is passed on to a variable. type of the function variable is "function".

JavaScript
<script>   let func_object = function (a, b) {     return a + b;   };   console.log(typeof func_object); </script> 

Output:

"function"

Next Article
How to get the native type of a value in JavaScript ?
author
isitapol2002
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • How to return the data type of variable in JavaScript ?
    To return the JavaScript data type of a variable we can use the JavaScript typeof operator. In JavaScript, unlike many other programming languages, we do not specify the type of a variable while declaring it, rather the variable's type is automatically inferred based on the value it holds. In other
    3 min read
  • How to get the native type of a value in JavaScript ?
    JavaScript variables can store any type of value. The JavaScript typeof operator can be used to find out the native type of value. It returns a string value indicating the type of the value. Syntax: typeof(value) Possible output values of typeof: TypeResultundefined"undefined"null"object"boolean"boo
    1 min read
  • How to Check the Data Type of a Variable in Ruby?
    In this article, we will discuss how to check the data type of a variable in Ruby. We can check the data types of a variable through different methods ranging from class to kind_of method in Ruby. Table of Content Using Class method Using is_a? Method Using kind_of? MethodUsing Class method The Clas
    3 min read
  • How to check if a Variable Is Not Null in JavaScript ?
    In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
    4 min read
  • How to Check the Type of an Object in Python
    In this article, we will explore the essential skill of determining the type of an object in Python. Before engaging in any operations on an object within the Python programming language, it is imperative to possess the knowledge of how to check its type. This fundamental task is encountered regular
    4 min read
  • How to Check the Type of an Object in Typescript ?
    When working with TypeScript, understanding how to check the type of an object is crucial for ensuring type safety and maintaining code integrity. TypeScript, being a statically typed superset of JavaScript, provides several approaches to accomplish this task as listed below. Table of Content Using
    3 min read
  • How to check whether an object exists in javascript ?
    Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly. Here we have some common approaches t
    3 min read
  • How to check if a value is object-like in JavaScript ?
    In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
    4 min read
  • How to check if the value is primitive or not in JavaScript ?
    To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
    3 min read
  • How to check if the provided value is of the specified type in JavaScript ?
    To check if the provided value is of the specified type in JavaScript, we have multiple approaches. Below are the approaches used to check if the provided value is of the specified type in JavaScript: Table of Content Using Object.is() Using TypeOf OperatorApproach: Using Object.is() Object.is() det
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences