const Keyword / Datatypes In JavaScript

ยท

5 min read

const Keyword / Datatypes In JavaScript

Photo by Andrew Neel on Unsplash

In this episode, we will discuss the 'const' keyword. Later on, we'll explore the different data types present in JavaScript and delve into the 'typeof' operator. so let's start ๐Ÿš€

In JavaScript, the "const" keyword is used to declare constants. Once a value is assigned to a constant using "const", that value cannot be reassigned or changed throughout the code.

const MAX_VALUE = 100; // constant 
MAX_VALUE =200; // will get an error, cannot reassign a value.

Since the value assigned to a constant declared with "const" cannot be changed, you can still use that constant value in various calculations, including arithmetic operations.

For example:- To find the area of a circle.

// Area of a circle = pi*r*r
const pi = 3.14159;
const radius = 5;
const area = pi * (radius ** 2);
console.log("The area of the circle is: " + area);

When you declare a variable using const, you must initialize it with a value at the time of declaration. If you try to declare it without an initializer, you will get an error.

const myConstVariable;
// The following line would result in an error:
// Error: Missing initializer in const declaration.

Datatypes In Javascript ๐Ÿš€

Before discussing the various data types in JavaScript, we'll first hop into the 'typeof' operator.

The typeof operator in JavaScript is used to determine the data type of a variable or an expression.

for example:-

let name = "anjali";
console.log(typeof name); // string
let age = 8;
console.log(typeof age); // number

In this case, name is assigned a value of 'anjali,' and age is assigned a value of 8. If you were to use typeof name or typeof age, it would return 'string' for the variable name because it is holding a string data type, and for the variable age, it would return 'number' since it is holding a number data type.

Introduction To Datatype๐Ÿค˜

Data types in programming languages categorize the types of values that variables can hold.

Datatype are of two types :-

  1. Primitive Datatype

  2. Reference Datatype

In this episode, we will explore primitive data types and delve into the differences between primitive and reference data types. We'll further discuss this when we cover arrays.

so let's hop into primitive datatypes

  1. Number - In JavaScript, the number data type represents both integer and floating-point numbers.

     let age = 19;
     let floatNumber = 2.16;
     console.log(typeof age); // number 
     console.log(typeof floatNumber); // number
    
  2. string - In JavaScript, the string data type is used to represent text and is a sequence of characters (letters, numbers, symbols, or spaces). Strings are enclosed in either single quotes (' '), double quotes (" "), or backticks (` ).

     let name = "yashika"
     let aboutMe = `my name is yashika `
     console.log(typeof name); // string 
     console.log(typeof aboutMe); // string
    
  3. undefined - It represents a variable that has been declared but not yet assigned a value. When a variable is declared but not initialized, it automatically holds the value undefined.

     let name; // name variable is declared but not initialized .
     console.log(name) // undefined
    
  4. null - It's often used to signify deliberate non-existence or no value.

     let fName = null
     console.log(typeof fname) // object
     fname = "rajni" // can also change null value
    

    ๐Ÿ˜ถWhen you use the typeof operator on a null value in JavaScript, it returns "object". Why it is returning object not null?

    ๐Ÿ™‚So, basically it is a bug in javascript, this was a mistake.

    ๐Ÿ˜ถNow you guys were thinking that if it is a bug ๐Ÿ›then why not correct it? ๐Ÿ™‚The reason it hasn't been corrected is due to the extensive usage of JavaScript in various libraries and frameworks. Since this behavior has been consistent throughout the language's history, countless libraries and frameworks have been developed based on this quirk๐Ÿ›. If this bug gets corrected then the vast amount of existing codebases of javascript libraries and frameworks have to change.

  5. BigInt - In JavaScript, there is a limit to the size of numbers that we can check through

     console.log(Number.MAX_SAFE_INTEGER);
    

    represents the maximum safe integer that can be accurately represented in JavaScript.

If you need to work with numbers larger than Number.MAX_SAFE_INTEGER then you can use BigInt. It allows you to work with arbitrarily large integers without losing accuracy or encountering issues related to the standard number limitations.

let bigNumber = bigInt(12234454565555555);
// or 
let bigNumber=12234454565555555n  // or we can add n at last that will also represent bigint

// we can do any arithmetic operation 
//but both the number should have to be bigint 
let num1= 1234n;
let num2= 12345n;
console.log(num1+num2)
console.log(typeof num1+num2) // bigint
  1. symbol - Symbols in JavaScript are a unique and immutable data type. We'll delve further into symbols when discussing objects in JavaScript.

  2. boolean - a boolean is a simple data type representing true or false values. Booleans are often used for decision-making and control in programming. They help control the flow of a program by determining whether certain conditions are met or not.

     let isRaining = true;
     let isSunny = false;
    
     if (isRaining) { // condition -> true , if block will execute
         console.log("Remember to take an umbrella!"); // this will execute
     } else {
         console.log("No need for an umbrella today.");
     }
    

    we will see if-else statements as we go further with our learning.

๐Ÿ™ƒSo, we've covered all the primitive data types. Later, we'll explore reference data types and discuss the differences between reference and primitive data types.

In the next episode we will see the type conversion, and will hop into string and its methods.

Iโ€™m on @buymeacoffee If you like my work, you can buy me a coffee and share your thoughts https://www.buymeacoffee.com/yashika227x .

ย