Javascript typeof operator for type checking

Learn about Javascript’s typeof operator, the type checking operator, which allows you to report the type of an existing variable.

The list of operators that we have been offering for it is completed with the typeof operator, which we will see below.

We have been able to verify that, for certain operators, the type of data they are handling is important, since if the data is of one type, different operations will be carried out than if it is of another.

For example, when we used the operator +, if it was about numbers it added them, but if it was about character strings it concatenated them. So we can see that the type of data we are using does matter and that we will have to pay attention to this detail if we want our operations to be carried out as expected.

To check the type of a data, another operator can be used that is available from javascript 1.1, the typeof operator, which returns a text string that describes the type of the operator that we are checking.

Note: throughout our experience with Javascript we will see that many times it is more useful to change the data type of a variable before doing a test with typeof to see if we can use it as an operand. There are several functions to try to change the type of a variable, such as , which we will see later in . var boolean = true var numeric = 22 var numeric_float = 13.56 var text = “my text” var date = new Date() document.write(“
The type of boolean is: ” + typeof boolean) document.write(”
The type of numeric is: ” + typeof numeric) document.write(“
The type of numeric_floating is: ” + typeof numeric_floating) document.write(“
The type of text is: ” + typeof text) document.write(“
The type of date is: ” + typeof date)

See also  Question about images

If we execute this script we will obtain that the following text will be written on the page:

The type of boolean is: boolean

The type of numeric is: number

The type of numeric_float is: number

The text type is: string

The date type is: object

In this example we can see that the different types of variables are printed on the page. These can be the following:

  • boolean, for boolean data. (true or false)
  • number, for numerics.
  • string, for character strings.
  • object, for objects.
  • function, for functions.
  • undefined, for declared variables that have not been assigned values.

We want to highlight just two more details:

  1. Numbers, whether or not they have a decimal part, are always of the numeric data type.
  2. One of the variables is a bit more complex, it is the date variable, which is an object of the Date() class, which is used to handle dates in scripts. We will see it later, as well as the objects.
  3. Also note that functions in Javascript are treated like the

Recommendations with typeof

In our opinion it is not very appropriate to constantly ask the type of a variable, to do different things when it has one type or another. The normal thing is that you are clear about what type of operation you want to perform at a given moment and that, if you are not sure if you have received the variable of one type or another, convert it with a function such as parseInt() or parseFloat(), which we will see later.

See also  Primitive entities for algorithm development

The most useful case I can now remember is asking if a variable’s type is undefined, which can give you the answer as to whether or not the variable has been initialized.

variable let; if(typeof variable == ‘undefined’) { console.log(‘Variable is undefined, has no defined value); }

Another case that is often used a lot is when doing debugging, when you need to find errors in your code, due to erratic and unexpected behaviors. In these cases it is useful to consult the type of the variables, because sometimes in Javascript they bring you surprises that make your programs not work properly.

With this we have already finished seeing the list of operators that we can use in Javascript and in the next article we will get to know the .

Loading Facebook Comments ...
Loading Disqus Comments ...