Understanding Variables and Data Types in JavaScript

JavaScript is dynamic programming language. Variables and data types are foundational concepts in programming, serving as the building blocks for storing and manipulating information within a program. In JavaScript, understanding these concepts is essential for writing effective and efficient code.

In JavaScript, variables are used to store and manage data. They are created using the var, let, or const keyword.

// using "var" keyword
var a = 10;// number 
var b = "hello"; // string
var c = true // boolean

//using "let" keyword
let a = 10;// number 
let b = "hello"; // string
let c = true // boolean

//using "const" keyword
const a = 10;// number 
const b = "hello"; // string
const c = true // boolean

The var keyword has function-scoped or globally-scoped behavior.

The let keyword is used for block-scoped variables. It is commonly used for variables that may change their value.

The const keyword declares variables that cannot be reassigned. It is also block-scoped.

  • var and let create variables that can be reassigned another value.

  • const creates "constant" variables that cannot be reassigned another value.

  • Developers shouldn't use var anymore. They should use let or const instead.

Data Types

JavaScript is a dynamically typed (also called loosely typed) scripting language. In JavaScript, variables can receive different data types over time.

Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types:

// 
let a = 5; // a is now a number
a = "car"; // a is now a string
a = true; // a is now a boolean

The latest ECMAScript standard defines eight data types Out of which seven data types are Primitive (predefined) and one complex or Non-Primitive.

Primitive Data Types

The predefined data types provided by the JavaScript language are known as primitive data types. All types except Object define immutable values, which are represented directly at the lowest level of the language. We refer to values of these types as primitive values. Primitive data types are also known as in-built data types.

  • Number: JavaScript numbers are always stored in double-precision 64-bit binary format IEEE 754. Unlike other programming languages, you don’t need int, float, etc to declare different numeric values.

      //number
      let age = 23 ;
    
  • String: JavaScript strings are like sentences made up of a list of characters, essentially an "array of characters," such as "Hello world." The String type represents textual data and is encoded as a sequence of 16-bit unsigned integer values representing UTF-16 code units.

      let fruit = "apple";
      const string1 = "A string can be sentence !";
    
  • Boolean: The Boolean type represents a logical entity and is inhabited by two values: true and false.

      let isvalue = true; // true or false
    
  • Null: This type has only one value that is null.

      null
    
  • Undefined: A variable that has not been assigned a value is undefined.

      let a 
      console.log(a) //undefined
    
  • Symbol: Symbols return unique identifiers that can be used to add unique property keys to an object. These keys won't collide with keys from any other code that might add to the object.

      const symbol1 = Symbol();
    
  • BigInt: BigInt is a built-in object in JavaScript that provides a way to represent whole numbers larger than 2^53-1.

      const previouslyMaxSafeInteger = 9007199254740991n;
      const alsoHuge = BigInt(9007199254740991); // same as 9007199254740991n
    
  • NaN: The NaN global property is a value representing Not-A-Number. NaN ("Not a Number") is a special kind of number value that's typically encountered when the result of an arithmetic operation cannot be expressed as a number. It is also the only value in JavaScript that is not equal to itself.

Non-Primitive Data Types

The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types.

  • Object: It is the most important data type and forms the building blocks for modern JavaScript. objects can hold multiple values as properties. It is used to store various keyed collections and more complex entities.

      let school = {
          key:"value"
          name: 'Public School',
          location: 'Delhi',
          established: '1980',
      }
    
  • Array: An array is used to store multiple values in a single variable. It can hold various data types.

      let arrayName = [value1, value2,];
    

Here is basic of variable and datatype in JavaScript.

Thank you for reading !!

I hope you find this article useful. If you have any questions related to this article, please feel free to comment below.