banner



How To Use Typeof In Javascript

INTRODUCTION

A lot of information floods in when dealing with engineering science. This information must take a nature in which information technology can exist expressed. Data blazon is the way of expressing that nature. JavaScript supports 3 different categories of data. Chief, reference and special data types.

Primary supports the basic types similar number, string, etc. while referential supports Object, Array, and Function. Special data blazon goes with undefined and cypher.

WHY USE TYPEOF?


JS supports dynamic variables which means that the aforementioned variable tin hold different data types, similar objects!. Information technology's important to know the data type, in club to make sense of the data.


Typeof operator in JS, returns the data type of the variable. As variables are dynamic, they don't have a information type fastened to them while they are being declared or initialized, like in other languages like C,C# etc. Typeof is used to return the variable.


Primary data type (numbers, strings, Boolean)

            //primay data types var car = "string"; var number = 2; var flag = false; certificate.write(" ", typeof automobile, " "); certificate.write(typeof number, " "); document.write(typeof flag, " ");                      

This way,  yous can find the types of different variables without having to run big and messy lawmaking.

REFERENTIAL Data TYPE (OBJECT Array AND FUNCTION)


Objects and functions are heterogeneous. That means that they tin can agree multiple data types or more than precisely they can carry information of different types. Whereas, an assortment is homogeneous and has a contagious memory. That means all the variables will be of the same blazon.

FUNCTIONS


Functions can have attributes of their own. Through typeof, we can check either the part's attribute information type, the data type the function will render or if the variable is a function or not.

            function myfunc() {     var name = "codeleaks";     let num = 9;     return 1; } document.write(typeof myfunc());                      

this volition give a number as a effect because the return value is a number.

This is how you can bank check if its function or not

            certificate.write(typeof function myfunc(){});                      

OBJECTS

Object's data type, equally long as information technology'due south not null, will return "object". To check that we can use the post-obit lawmaking.

            const Object = { name: "codeLeaks" }; document.write(typeof Object);                      

ARRAY

Fifty-fifty though arrays are homogenous only they are considered equally objects in JS. A kind of an object in which typeof of an assortment will return as an object. In consequence, ES5 released a office  isArray() to differentiate between array and object.

            const array = [1, 2, 3, 4, 5]; certificate.write(typeof array); document.write(Array.isArray(Object));                      

Note that the second value is false because an object is not an array

special data type( zero and undefined)

Undefined occurs when a variable is declared and non initialized (no assigned value). Therefore, the type would exist undefined.

            var leaks; document.write(typeof leaks);                      

Hither in that location is no specific information type or value assigned to the variable. Therefore, the output volition be undefined because information technology tin not differentiate the container's nature for holding any value.

You can even check the data type of undefined like this, it volition give the same result.

            console.log(typeof undefined);                      

Null'due south data type is an object. This is still considered a issues because null's purpose is to check for an empty variable and shouldn't have the information blazon equally an object. Therefore, typeof can't exist used with nix. We can use strict (===) or loose (==) equality operators for naught

            var leaks;  document.write(typeof nada, " "); document.write(leaks == null, " "); document.write(leaks === null);                      

The typeof of null is returned as an object, a issues. Strict equality operator checks the information type and value of the comparands. Hence, returning fake because information type of zero is an object while information blazon of variable is undefined, which are non equal.

Determination

Typeof is vastly used as its like shooting fish in a barrel to utilize without the hassle of writing big codes or functions. Typeof is as well helpful when dealing with referential information types but has its limits with cipher information type.It's excellent for agreement the departure betwixt null and undefined.  Still proving to be a handy and a strong tool.

How To Use Typeof In Javascript,

Source: https://www.codeleaks.io/how-to-check-data-types-in-javascript-using-typeof/

Posted by: cartwrightpospot.blogspot.com

0 Response to "How To Use Typeof In Javascript"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel