codedamn logo
codedamn

typeof operator in JavaScript- How to use it?

  • Vishnupriya's profile image
    Vishnupriya
    Team codedamn
typeof operator in JavaScript- How to use it?

Hey readers, in this article we will be discussing how to use typeof operator in JavaScript in a step-wise, efficient manner. If you are a beginner and want to learn JavaScript then do check out the course on Codedamn.

Introduction to typeof operator

JavaScript is one of the most used languages when it comes to building web applications as it allows developers to wrap HTML and CSS code in it to make web apps interactive. It allows a web application to be interactive. It is also used for making animations on websites and has a large community on GitHub. JavaScript has tons of libraries, one of which is React which we will be covering in this article later. 

Use cases: 

  • Building web server and its interactive functions
  • Animations and graphics, adding special effects to web components
  • Validating forms and exception errors
  • Adding behavior and functionalities to web pages

Since JavaScript is a scripting language, it works without any libraries. Developers are free to write JavaScript language code anywhere and can still run it in the browser. There is no structure in JavaScript, the browser executes each line of the code from top to down.

The typeof operator in JavaScript returns the data type of its operand as a string. Any object, function, or variable can be used as the operand.

Syntax:

typeof operand

OR

typeof (operand)

The operand is an expression that represents the object or primitive that will be returned.

In javascript, there are several types that can be used:

  • undefined
  • Object
  • boolean
  • number
  • string
  • symbol
  • function
<script>
// "string"
document.write(typeof 'mukul'+"<br>")
// "number"
document.write(typeof 25 +"<br>")
// "undefined"
document.write(typeof variable)
</script>

Let’s go over each type one by one, with a separate code section for each one.

In this example, we utilized the ‘===’ (strict equality comparison operator), which compares both value and type and returns true or false. Take, for example, the first console. The js compiles from left to right, first calculating the type of 25, which is ‘number,’ then comparing it to ‘number,’ and finally returning true or false as appropriate.

The typeof operator comes in handy since it allows you to quickly determine the type of a variable in your code. Because JavaScript is a dynamically typed language, this is critical. This means that when you create variables, you don’t have to assign them types. A variable’s type can vary during the course of a program because it is not constrained in this way.

The typeof operator returns a string that denotes a variable’s current type. Type typeof(variable) or typeof variable to utilize it. Returning to the preceding example, it may be used to determine the type of variable x at each stage:

Another application of the typeof operator is to ensure that a variable is defined before accessing it in your code. This can assist prevent program problems caused by trying to access a variable that isn’t specified.

Type of the operand Result
object “object”
number “number”
string “string”
boolean “boolean”
function “function”
undefined “undefined”
<html> 
<head> 
<script> 
document.write(typeof null + "<br>"); // results: "object" 
document.write(typeof [1, 2, 'hello'] + "<br>"); // results: "object" 
document.write(typeof {a: 'hello'} + "<br>"); // results: "object" 
document.write(typeof [1, 2, 3, 4] + "<br>"); // results: "object" 
document.write(typeof function(){} + "<br>"); // results: "function" 
document.write(typeof class hello{} + "<br>"); // results: "function" 
</script> 
</head> 
<body> 
</body> 
</html> 

The operands in this example are of the types Object and Function. Depending on the operand type, the typeof operator will print the “object” and “function.”

Conclusion

This was about the typeof operator in JavaSript using HTML, if you want to learn more about javascript, do check out the article and course on Codedamn of javascript along with the course. Hope you liked this, if you have any queries or suggestions do let us know in the comments. 

If you are interested in learning JavaScript, do check out courses on codedamn with an in-built environment playground to learn and practice code. Join the community of codedamn and do check out other articles related to programming and development on codedamn and subscribe to our newsletter to never miss out on our new programs and updates.

If you have any queries or feedback do let us know in the comment section.

User avatar