JavaScript is a dynamic programming language that allows the creation of interactive and complex content on websites. When we talk about data types in JavaScript, we are referring to the different types of values that a variable can have. Data types are fundamental to understanding how information is stored and manipulated in JavaScript.
Variables
A variable is a container for storing data. In JavaScript, we declare variables using the keywords 'var', 'let' and 'const'. For example, let name = 'John';
. Here 'name' is the variable and 'John' is the value we are storing in it.
Data Types
JavaScript has six primitive data types: String, Number, Boolean, Null, Undefined, and Symbol. Additionally, it has a non-primitive data type: Object.
String
A string is a sequence of characters used to represent text. In JavaScript, strings are wrapped in single or double quotes. For example, let name = 'John';
or let name = "John";
.
Number
The Number data type is used to represent positive or negative numbers with or without decimals. For example, let age = 25;
or let average = 19.5;
.
Boolean
The Boolean data type has only two values: true or false. This data type is commonly used for conditional testing. For example, let isAdult = true;
.
Null
Null is a special data type that represents "nothing" or "empty". For example, let empty = null;
.
Undefined
A variable that has been declared but has not had a value assigned is of type undefined. For example, let test;
Here, test is undefined.
Symbol
Symbol is a data type introduced in ES6 that produces a unique value that cannot be changed. For example, let sym1 = Symbol('sym');
.
Object
Objects are used to store collections of data and more complex entities. They are different from primitive data types because they can contain multiple values in the form of properties. For example, let car = {make: 'Toyota', model: 'Corolla', year: 2005};
.
Operators
Operators are used to perform operations between variables and values. The main types of operators in JavaScript are: arithmetic operators, assignment operators, comparison operators, logical operators and bitwise operators.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations. For example, let sum = 10 + 20;
.
Assignment Operators
Assignment operators are used to assign values to variables. For example, let x = 10;
.
Comparison Operators
Comparison operators are used to compare two values. For example, let result = (10 == 20);
Here, result will be false.
Logical Operators
Logical operators are used to determine the logic between variables or values. For example, let result = (10 < 20 && 20 > 30);
Here, result will be false.
Understanding variables, data types and operators is fundamental for anyone who wants to become a front-end developer. They are the foundation of any JavaScript program and are used in almost every script you will write.