JavaScript simply refers to the various types of data you will store in variables to support the accomplishment of specific tasks in your program as “data types.” JavaScript variables hold a single value, in contrast to object variables, which store multiple pieces of data.
Even though a variable can only hold one value at a time, it can store different data types. These include strings, numbers, and others. You must familiarize yourself with each of these data types to prevent data from being stored in the wrong format, which could cause issues with your code.
Understanding the Concept of Data Types in JavaScript
Data types is a crucial concept in programming that the computer needs to solve problems securely.
Similarly, adding a string to an int value will result in an error when compiled (let x = 61 + “nitro”;). JavaScript treats the number as a string when adding a number and a string.
In JavaScript, eight fundamental data types fall into three broad categories:
Primitive | Composite (or reference) | Special data types |
String Number Boolean | Object Array Function | Undefined Null |
Unlike composite data types, which can store collections of values and more complex entities, primitive data types can only hold one value at a time.
1. Strings
A “string” is a collection of alphabetic characters representing certain character sequences in text. One or more characters are enclosed in single or double quotes to form strings, as shown below:
Example
var greet = 'Hello dear, are you there!'; // the code here is using single quotes
var greet2 = "Hi dear, are you there!"; //we use double quotes here
var a = "Can we go to the amusement park? "; // double quote embedding single
var b = 'He said "YES."'; // single quotes embedding double
var c = 'We\'ll go out once it is 1 pm.'; // escaping backslash
2. Numbers
Numbers written in exponential notation, such as 1.5e-4 (equivalent to 1.5×10-4) or positive or negative numbers with or without a decimal place, are represented using the number data type.
Example
var a = 255; // this is an integer
var b = 10.5; // this is a floating-point number
var c = 2.45e+6; // exponential notation
var d = 9.15e-6; // exponential notation
3. Boolean Data Type
Only true or false are valid values for the Boolean data type. According to the example given below, it is frequently used to store values such as yes (true) or no (false), on (true) or off (false), etc.
Example
var isEating = true; // means that there is such action
var isEating = false; // means that there is no such action
4. The Null Data Type
This is yet another unique data type that only accepts the null value as a value. If it says null, then it means emptiness.
You can explicitly empty a variable of its current contents by giving it the null value.
5. The Undefined Data Type
The only possible value for the undefined data type is the singular value undefined. Undefined values are associated with variables that have been declared but not given a value.
Example
// initialize a but no value was added
var a;
// initialize b with value added
var b = "Hello community!"
console.log(a) //Output: undefined
console.log(b) // Output: Hello community
6. The Object Data Type
The object is a sophisticated data type that enables the storage of data collections.
Properties, which are key-value pairs, are features of objects. These include strings, numbers, booleans, and complex data types like arrays, functions, and other objects that can be used as a property’s value.
Example
// initialize emptyObj but no value was added
var emptyObj = {};
// initialize person1 objects variable with value added
var person1 = {"name": "Pele", "surname": "Maradona", "age": "76"};
7. The Array Data Type
An object type known as an array is used to store numerous values in a single variable. Each value or element in an array has a numeric position, known as its index, and can hold data of any data type, including numbers, strings, booleans, functions, objects, and even other arrays. The first array element is arr[0] rather than arr[1] because the array index starts at 0.
Example
// initialize an array with values
var colors = ["pink", "blue", "orange", "sky blue", "red"];
var cities = ["Asmara", "Mitsiwa", "Āssab", "Keren"];
8. The Function Data Type
A callable object that runs a block of code is called a function. It is possible to assign functions to variables because they are objects.
Example
// this function will return a greeting
var greeting = function(){
return "Hello dear, how are you today";
}
console.log(greeting());
It’s possible to store functions in variables, objects, and arrays. Functions can be returned from other functions as well as passed as arguments to other functions.
Conclusion
In this article, you learned about JavaScript data types. Next, we will be looking at JS objects.