Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

JavaScript - Variables and Data Types

Variables and Data Types

Variables and data types are fundamental concepts in programming. They allow you to store and manipulate different types of information in your code. In JavaScript, there are several data types you can use. Here's an introduction to variables and data types in JavaScript:

Variables:

A variable is a named container used to store data values. In JavaScript, you can declare variables using let, const, or var. Here's an example:

         let name = "John";
const age = 30;
var isStudent = true;

 

  • let: Allows you to declare variables that can be reassigned.
  • const: Declares variables that cannot be reassigned. The value is constant.
  • var: Older way of declaring variables. It's function-scoped and not recommended in modern JavaScript.

Data Types:

JavaScript has several data types, categorized into two main groups:

1. Primitive Data Types:

These are basic, immutable data types that directly operate on their values.

  • String: Represents text. Example: "Hello, World!".
  • Number: Represents numeric values. Example: 42.
  • Boolean: Represents true or false values. Example: true.
  • Undefined: Represents a variable that has been declared but not assigned a value. Example: let x;.
  • Null: Represents the absence of a value. Example: let y = null;.
  • Symbol (ES6): Represents a unique and immutable value, often used as object property keys.
  • BigInt (ES11): Represents large integers that cannot be represented by the Number type.

2. Reference Data Types:

These are more complex data types that hold references to memory locations.

  • Object: Represents a collection of key-value pairs (properties and methods).
  • Array: Represents an ordered list of values.
  • Function: A block of reusable code that can be called with specific arguments.

Dynamic Typing:

JavaScript is a dynamically typed language, which means that you don't need to explicitly specify a variable's data type. The type is determined at runtime based on the value it holds.

let message = "Hello"; // message is a string
message = 42; // now message is a number
       

 

Type of Operator:

You can use the typeof operator to determine the data type of a variable:

let name = "John";
console.log(typeof name); // Output: "string"