How To Check Variable – Javascript

When dealing with a variable in Javascript, from time to time we do need to verify the type of variable we are dealing with. The most common type of a variable in Javascript that we would most likely face in every program are the following: “string, number, boolean, null, undefined, function, object, and array”.

As the name defined, string type variable will store a string of character. When numbers are stored in a string they are not numbers but now a character value. Number type variable will only store mathematical number. Boolean type variable will store either true/false or 1/0. Undefined and Null variables to its name are not defined or have a null property. Objects can store set of properties, and each property can store differents value. Object’s properties can be of any type mention previously. Array type variable is almost similar to Object type variable. However, their properties do not go by name but go by index starting at the zero index. You do not need to define the index number for an Array. However, it’s better to do so in some instance.

For basic variables checking we can use typeof(), the typeof function in javascript will return the name type of the variable in string format.

if ( typeof(variable) === "string"){  //Notice how I have to use double quotation here. Because the return value is in string format
  alert("I am a string");
}

Here is an example of how you alert the type of the variables.

   alert( typeof(variable) ); 

Here is an example of a conditional block that utilise the usage of typeof() function.

if ( typeof(variable) === "number") {
    variable = variable + 10;
    alert(variable);
}
if ( typeof(variable) === "string") {
    variable = "Isn't string cool. I can store more information than number. " + variable;   
    alert(variable);
}

The typeof() function can check for “string, number, boolean, null, undefined, function, and object. The drawback of the typeof() function is, it’s can not check for an Array. This is due to the fact that array also counts as an object for the typeof() function. If you pass to the typeof() function an Object or an Array. The typeof() function will tell you that they are both Objects.

So how do we check for an Array? There is an instanceof keyword that will check for Object or Array, but you must use the instanceof keyword properly to show it’s full potential. The condition that Javascript will treat an Array as an instance of Object will also apply here just with some minimal changes. An Array is an instance of Object. However, an Object is not an instance of Array. When you use the instanceof keyword, the type naming must be in the format of first letter capitalized. For example, where typeof() will return “object”, in instanceof case you would have to put Object. Instanceof will return only true or false.

This example code block will tell you that for sure this is an array.

variable = [];
if ( variable instanceof Array ){
    alert("I am an array");
}
alert(variable instanceof Array);

This sample code below will check for sure if the variable is an Object 100% and is not an Array. This is something that typeof() function can’t do. Notice the ! in the condition. That ! in the condition means “is not” or “the opposite of”.

variable = {};
if ( variable instanceof Object && !(variable instanceof Array) ){
    alert("I am an object 100%, do not think that I am an array");
}
alert(variable instanceof Object && !(variable instanceof Array) );

This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.
Original Post Name: "How To Check Variable – Javascript".
Original Post Link: http://kevinhng86.iblog.website/2017/01/17/basic-variable-checking-javascript/.

Advertisement


Random Article You May Like

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*