DEV Community

Raghul
Raghul

Posted on • Edited on

Variables and Datatypes in JavaScript

Variables and Datatypes in JavaScript

Variables and Data Types in JavaScript are fundamental concepts used to store and manage data in a program. They define how information is declared, stored, and manipulated during execution.

  • Variables: Declared using var, let, and const to store data values.
  • Primitive Data Types: Includes Number, String, Boolean, Null, Undefined, BigInt, and Symbol.
  • Non-Primitive Data Types: Includes Object, Array, and Function used to store complex data.

Variables

A variable is like a container that holds data that can be reused or updated later in the program. In JavaScript, variables are declared using the keywords var, let, or const.

  1. var Keyword The var keyword is used to declare a variable. It has a function-scoped or globally-scoped behaviour.
var n = 5;
console.log(n);

var n = 20; // reassigning is allowed
console.log(n);
Enter fullscreen mode Exit fullscreen mode
  1. let Keyword The let keyword is introduced in ES6, has block scope and cannot be re-declared in the same scope.
let  n= 10;
n = 20; // Value can be updated
// let n = 15; //can not redeclare
console.log(n)
Enter fullscreen mode Exit fullscreen mode
  1. const Keyword The const keyword declares variables that cannot be reassigned. It's block-scoped as well.
const n = 100;
// n = 200; This will throw an error
console.log(n)
Enter fullscreen mode Exit fullscreen mode

4.without keyword
In this we can reinsilate and it prints the last value declare.

i=10;
i=20;
console.log(i)
Enter fullscreen mode Exit fullscreen mode

Data Types

JavaScript supports various datatypes, which can be broadly categorized into primitive and non-primitive types.

Primitive Datatypes
Primitive datatypes represent single values and are immutable.

  1. Number: Represents numeric values (integers and decimals).
let n = 42;
let pi = 3.14;
Enter fullscreen mode Exit fullscreen mode
  1. String: Represents text enclosed in single or double quotes.
let s = "Hello, World!";
Enter fullscreen mode Exit fullscreen mode
  1. Boolean: Represents a logical value (true or false).
let bool= true;
Enter fullscreen mode Exit fullscreen mode
  1. Undefined: A variable that has been declared but not assigned a value.
let notAssigned;
console.log(notAssigned);
Enter fullscreen mode Exit fullscreen mode
  1. Null: Represents an intentional absence of any value.
let empty = null;
Enter fullscreen mode Exit fullscreen mode
  1. Symbol: Represents unique and immutable values, often used as object keys.
let sym = Symbol('unique');
Enter fullscreen mode Exit fullscreen mode
  1. BigInt: Represents integers larger than Number.MAX_SAFE_INTEGER.We can denote BigInt by using n at the end or the values.
let bigNumber = 123456789012345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

Non-Primitive Datatypes
Non-primitive types are objects and can store collections of data or more complex entities.

  1. Object: Represents key-value pairs.
let obj = {
    name: "Amit",
    age: 25
};
Enter fullscreen mode Exit fullscreen mode
  1. Array: Represents an ordered list of values.
let a = ["red", "green", "blue"];
Enter fullscreen mode Exit fullscreen mode
  1. Function: Represents reusable blocks of code.
function fun() {
    console.log("GeeksforGeeks");
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)