Some JavaScript functions are used to check data type

Tram Ho

Each programming language has its own corresponding data types. In Javascript too, we have two large groups: primitive data types and reference data types. Correspondingly we will have primitive data types such as string , number , boolean , null , undefined , symbol and reference data types such as object , array , function . Each data type will have its own characteristics that other types do not have. Therefore, if we do not carefully check the input data type of an action, there is a possibility that our code will cause bugs, even crash the system. Today let’s try to learn some function in Javascript to minimize this, help us always determine the correct input of our processing.

I. Has the input check been declared?

When we write a function to handle a certain job. It is really necessary to check whether the function’s input variable has been declared or not. So when writing code we will branch accordingly for the declared and undeclared instance. At the same time when there are abnormal input data, our processing still produces the desired output instead of causing bugs or crashes.

Checking the declaration of the input will help our code work more efficiently with abnormal input data. It also helps us to divide processing easier and the probability of bugs will be less.

So let’s carefully examine the input declaration. Although we will code more, it will save time investigating and handling bugs later (if dealing with bugs).

II. Check the correct data type as desired?

In the first part we took the step of verifying the declaration. And it makes our code more efficient, reducing bugs and unexpected crashes. But besides that when we write a script we all want it to work properly. Therefore, checking the data type of the input is also a prerequisite for proper processing.

Above are some functions used to check primitive data type and reference type (object). Next, let’s add some other data type checking functions.

We have gone through a number of functions that help ensure the correct input of the actions, create the direction of anomalies, reduce bugs and crashes.

Its effects are very good so please apply it to the code and in combination with Unit test it will help our code be clearer, more transparent and cover as much of anomalous as possible. .

III. Conclude

So my article here is over. Hope it is a little but also helps you a certain part in your working process. In the following article we will take a look at some of the functions that are useful when working with arrays as well as some of the more commonly used techniques in arrays. Thank you for your reading. See you in the next article. Hello!!!

Share the news now

Source : Viblo