Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to check variable type in javascript

Tags:

<script type="text/javascript">    function saveName (firstName) {     function capitalizeName () {         return firstName.toUpperCase();     }     var capitalized = capitalizeName();console.log(capitalized instanceof String);     return capitalized;  } console.log(saveName("Robert")); // Returns "ROBERT" </script> 

Question:

I want to check the type of capitalized , so I use capitalized instanceof String? But it shows: false in console, I do not want to try capitalized instanceof Function, Object...It will take too much time, so what is the best way to detect a variable type?

like image 916
user2507818 Avatar asked Jul 03 '13 05:07

user2507818


People also ask

How do you check what type a variable is JavaScript?

typeof: The typeof keyword helps to determine the type of a variable in Javascript. Since Javascript is a dynamically typed programming language, typeof can be used to find the variable type. It can be used within a function to check the data type of a variable or to check if a variable is declared.

How do you check the type of a variable?

To check the type of a variable, you can use the type() function, which takes the variable as an input. Inside this function, you have to pass either the variable name or the value itself. And it will return the variable data type.

What is a variable type in JavaScript?

Variables can be used to store data in a program, such as strings, numbers, JSON objects, or boolean values. In JavaScript, there are three different variable types: var , let , and const . Each of these variables have several rules around how they should be used, and have different characteristics.

How do you check if a variable is a function JavaScript?

To check if a variable is of type function, use the typeof operator, e.g. typeof myVariable === 'function' . The typeof operator returns a string that indicates the type of the value. If the type of the variable is a function, a string containing the word function is returned. Copied!

How to check if a variable is a string in JavaScript?

let's say that you want to test or check if a variable is a string, see the code bellow As you can see, the code works very well, but this is not the best way to do that. Remember!, if you want to check the type of variable, you should use isinstance () built function.

Is it possible to check the type of a variable?

As you can see, the code works very well, but this is not the best way to do that. Remember!, if you want to check the type of variable, you should use isinstance () built function.

How to check the type of an object in JavaScript?

Using instanceof operator: The instanceof operator checks the type of an object at run time. It return a corresponding boolean value, i.e, either true or false to indicate if the object is of a particular type or not.

Is there a variable type for variables in JavaScript?

Javascript is known to have no types for variables. A really nice thing. But somehow it must determine the type and work with it. var a = 1; var b = 2.0; var c = 'c'; var d = "Hello World!"; So what we have is an Integer, Double/Float, Character, String (which may be teared down as char*)


2 Answers

The best way is to use the typeof keyword.

typeof "hello" // "string" 

The typeof operator maps an operand to one of six values: "string", "number", "object", "function", "undefined" and "boolean". The instanceof method tests if the provided function's prototype is in the object's prototype chain.

This Wikibooks article along with this MDN articles does a pretty good job of summing up JavaScript's types.

like image 164
LandonSchropp Avatar answered Oct 12 '22 10:10

LandonSchropp


use typeof();

example:

> typeof "foo" "string" > typeof true "boolean" > typeof 42 "number" 

So you can do:

if(typeof bar === 'string') {    //whatever } 

Keep in mind that, typeof is only good for returning the "primitive" types, number, boolean, object, string. You can also use instanceof to test if an object is of a specific type.

function MyObj(prop) {   this.prop = prop; }  var obj = new MyObj(10);  console.log(obj instanceof MyObj && obj instanceof Object); // outputs true 
like image 39
Fasil kk Avatar answered Oct 12 '22 09:10

Fasil kk