Data Types
Most programming languages support various data types, for example, integer, string, boolean, etc. Data types in JavaScript play an important role in declaring data that could be used in the program for operations. Data types are the type of data which stores data in a variable. They are the set of values from which an expression may take values.
For a beginner, it's very important to know that data types are the basic fundamentals which play an important role. Basics in every programming language are almost the same. Hence, you need to have a good grip on the basic fundamentals before moving to the advanced concepts. Let's move on to the data types in JavaScript -
Data Types in JavaScript
- Primitive data types or built-in data types (number, string, boolean, undefined, null, symbol)
- Non-Primitive data types or derived data types (object and arrays)
Syntax to declare a variable using a data type -
data_type variable = data/value;
JavaScript supports several data types which includes:
1. String
String is a data type in JavaScript which is a collection of characters. It is used to represent text in JavaScript, enclosed in single or double quotes.
Example
let name = "Isha";
2. Number
Another important data type in JavaScript is number which is used to store literal value. Numbers are stored in variables denoted by a data type (in let and const).
Example
let value = 10;
3. Boolean
A data type which is used to define a statement by being true or false. They are often used in conditional statements to declare whether a statement is true or false.
Example
let a = true;
4. Undefined
A variable which has no value is undefined. It represents the value of a variable that has not been assigned a value.
Example
let name; // where name is not defined(holds no value).
5. Null
Null is another data type that represents empty or null value. It represents the intentional absence of any object value.
Example
const a = null;
6. Symbols
Values that cannot be changed. A new data type in JavaScript as of ES6. Also, they are used to create unique identifiers.
Example
const name = Symbol("Guvi");
7. Object
A data type which is used to store value in an object. They are used to represent more complex data structures. They can contain properties and methods.
Example
const person = {firstName: "Guvi" , lastName: "Geeks"};
8. Arrays
Arrays are a type of an object in JavaScript that can hold a collection of values, including other arrays. To create an array in JavaScript, here's an example:
Program:
var number = [1,2,3,4,5];
console.log(number[2]);
3
These are the most common data types in JavaScript. It's important to note that JavaScript is a dynamically-typed language, which means that variables can hold different data types at different times.
Conclusion
In this module, we have discussed the Introduction of JavaScript, its features, syntax, variables, EcmaScript6 (let and const), and data types. You'll also be more clear with the syntax and given examples.