In programming, data types are used to define the type of data that a variable can hold. A variable is a named container that stores a value.
In JavaScript, there are several data types, including:
Data Type | Description | Example |
---|---|---|
string | A sequence of characters | “Hello World” |
number | A numerical value | 42 |
boolean | A true/false value | true |
null | A variable with no value | null |
undefined | A variable that has not been assigned a value | undefined |
object | A complex data type that can hold multiple values | { name: “John”, age: 30 } |
symbol | A unique value that cannot be changed | let id = Symbol() |
Variables can be declared using the var
, let
, or const
keywords. var
and let
can be used to declare variables that can be reassigned, while const
is used to declare constants that cannot be reassigned.
Example program:
// Declare a string variable
let message = "Hello World";
// Declare a number variable
let age = 42;
// Declare a boolean variable
let isStudent = true;
// Declare a null variable
let x = null;
// Declare an undefined variable
let y;
// Declare an object variable
let person = { name: "John", age: 30 };
// Declare a symbol variable
let id = Symbol();
console.log(message);
console.log(age);
console.log(isStudent);
console.log(x);
console.log(y);
console.log(person);
console.log(id);
Output:
Hello World
42
true
null
undefined
{ name: 'John', age: 30 }
Symbol()
To run this program in Visual Studio Code IDE after installing node.js:
- Open Visual Studio Code
- Create a new file and save it with a
.js
extension (e.g.data_types.js
) - Copy and paste the code above into the file
- Open the terminal in Visual Studio Code (View > Terminal)
- Type
node data_types.js
in the terminal and press Enter - The output should be displayed in the terminal window.