I know sometimes JavaScript can be confused with Java because of the name or for its similarity with the syntax but definatively data type is one of the javaScript properties that differ from Java.
JavaScript variables can hold many types: objects, Strings , numbers, boolean and so on, this concept it is very important in programming because made the execution categorized and organized to follow.
Numbers: is a data type in java that consisting as its name says it in numbers in java and other languages we have data types like integers or float or doubles, but in java all falls in that category example:
var x = 16; // this is a number
var y = 16.586934 ; //this is a number too
String: This data have a rule, its value always have to be surround by quotes, a string is also known as array of characters group together example:
let str = "Hello World"; // this a string
let str = "number: 123"; // this also is a string
Objects: This is a special case because objects main role is to store collection of data basically you can store more than one type inside it example an Array is an object, which can contain many data types inside example:
let cars = ["Saasb", false, 2345,]; // this is an object
let person = {firstName:"John", lastName:"Doe", age:50}; // this is also an object
Boolean: This is a logical data type and only has two values “true” and “false” example:
let isMarried = true; // this is a boolean
let isOn = false; // this is a boolean
let isGreater = 50 > 45; // this will return true , which is also a boolean
Null: This is also another special case , because the null value basically mean “nothing” it is use to clean other types example:
let age = 2019 - 1990;
age = null; // we are setting the age to null in a way to clean it
Undefined: Another case like “null” undefined values is given to a variable that has not value assigned example:
let age; // this will print out undefined because has not value assigned
Just case you are not sure or don’t know what data type or what category falls a data type in javaScript, there is a method for you this method is called typeOf example:
typeof( 0); // "number"
typeof( true); // "boolean"
typeof( "foo"); // "string"
typeof undefined // "undefined"
This is a very helpful tool in JavaScript when we are trying to differ numbers from string and want to categorize it but it is also good to know the data types and what values they can hold.