Software Development

Type Checking in JavaScript

Introduction

[wp_ad_camp_5]

Okay, JavaScript is a loosely-typed language, which means you do not declare the data type of a variable explicitly. In a basic variable assignment expression, e.g., var i = 4;, the value that assigns to the variable determines the data type of the variable. As you assign different types of data to the same variable, its data type changes as well. Given the nature of JavaScript, finding out what data type a variable represents before using it is very important.

JavaScript Codes

To determine the data type of a variable (or value to be assigned to it), we use typeof. Here are some examples:

Outputs:

[wp_ad_camp_3]

As you can see, typeof against an array and object results to ‘object’. To check whether a variable (or value) is an array or object:

Outputs:

[wp_ad_camp_2]

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like