How to check Data types in JavaScript using typeof:
The typeof keyword is used to differentiate primitive types in JavaScript. It will returns one of nine strings:
undefined, object(meaning null), boolean, number, bigint, string, symbol, function, or object(meaning any object, including arrays).
The typeof
operator is useful because it is an easy way to check the type of a variable in your code. This is important because JavaScript is a dynamically typed language. — freeCodeCamp
The typeof
statement is really useful in JavaScript for data validation and type checking, but it has some odd features to be aware of.
Specifically, there are two instances when typeof
returns "object"
in an unexpected way, and this makes type checking a little unusual.
Both typeof
null
and typeof
an array return "object"
in a potentially misleading way, as null
is a primitive type (not an object), and arrays are a special, built-in type of object in JavaScript.
Try the rest countries api(Application Programming Interface):
First of all we know what is api?
Application Programming Interface (APIs) are constructs made available in programming languages to allow developers to interact with other systems/application to get data
//create a request variable
var request = new XMLHttpRequest();//open a connection
request.open('GET','https://restcountries.eu/rest/v2/all',true)//send the request
request.send();//load the response
require.onload = function(){
var data = JSON.parse(this.response);
console.log(data);
}