DEV Community

Cover image for JS Variables, Operators, Data Types
Jahid.Dev
Jahid.Dev

Posted on

JS Variables, Operators, Data Types

  1. JavaScript Variables can be declared in 4 ways -
    Automatically, Using var, Using let, Using const

  2. The var keyword was used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. The var keyword should only be used in code written for older browsers. Variables are containers for storing values.

  3. All JavaScript variables must be identified with unique names.
    These unique names are called identifiers. JavaScript identifiers are case-sensitive.

  4. The "equal to" operator is written like == in JavaScript. Creating a variable in JavaScript is called "declaring" a variable. After the declaration, the variable has no value (technically it is undefined).

  5. You can declare many variables in one statement. Start the statement with let and separate the variables by comma:
    let person = "John Doe", age = 25, color = "Gray";

  6. JavaScript scoping determines the visibility and lifetime of variables within a program. Understanding scoping is essential for writing efficient and bug-free code. Here’s an overview of the different types of scoping in JavaScript -
    Global Scope -
    Variables declared outside any function or block are in the global scope and are accessible from anywhere in the code. In a browser, global variables become properties of the window object.
    Function Scope -
    Variables declared inside a function are in the function scope and are not accessible outside the function. JavaScript functions create their own scope.
    Block Scope -
    Introduced in ES6, let and const allow block-level scoping. This means variables declared within a block (e.g., inside an if statement or a loop) are only accessible within that block.
    Lexical Scope -
    JavaScript uses lexical scoping (or static scoping), meaning the scope of a variable is determined by its position within the source code. Nested functions have access to variables declared in their outer scopes.
    The this Keyword -
    The value of this depends on the context in which a function is called. In the global scope, this refers to the global object (window in browsers). Inside a function, its value depends on how the function is called.
    Global Scope: Variables accessible from anywhere.
    Function Scope: Variables accessible only within the function.
    Block Scope: Variables accessible only within the block (using let and const).
    Lexical Scope: Inner functions have access to variables of their outer functions.
    this Keyword: Depends on the context of the function call.

  7. Variables declared with let have Block Scope. Variables declared with let must be Declared before use. Variables declared with let cannot be Redeclared in the same scope.

  8. let and const have block scope. let and const can not be redeclared. let and const must be declared before use. let and const does not bind to this.

  9. Variables defined with const cannot be Redeclared, cannot be Reassigned and have Block Scope.

  10. Variables declared with let and const are hoisted to the top of their block scope (not function or global scope), but they are not initialized. Using let variable before it is declared will result in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until it is declared. The 'var' is hoisted. The Temporal Dead Zone (TDZ) in JavaScript refers to the period of time during which a variable is declared using let or const but has not yet been initialized.

  11. Use const when you declare - A new Array, Object, Function, RegExp.

  12. Assignment Operator =
    Addition Operator +
    Multiplication Operator *
    Comparison Operator >

  13. There are different types of JavaScript operators -
    Arithmetic Operators
    Assignment Operators
    Comparison Operators
    String Operators
    Logical Operators
    Bitwise Operators
    Ternary Operators
    Type Operators

  14. Addition +
    Subtraction -
    Multiplication *
    Exponentiation **
    Division /
    Modulus(Division Remainder) %
    Increment ++
    Decrement --
    Addition Assignment Operator +=

  15. Comparison Operators -
    equal to ==
    equal value and equal type ===
    not equal !=
    not equal value or not equal type !==
    greater than >
    less than <
    greater than or equal to >=
    less than or equal to <=
    ternary operator ?

  16. JavaScript Type Operators -
    typeof - Returns the type of a variable.
    instanceof - Returns true if an object is an instance of an object type.

  17. Arithmetic Operators -
    Addition +
    Subtraction -
    Multiplication *
    Exponentiation (ES2016) **
    Division /
    Modulus (Remainder) %
    Increment ++
    Decrement --

  18. Assignment Operators -
    A) =
    B) +=
    C) -=
    D) =
    E) /=
    F) %=
    G) *
    =

  19. JavaScript has 8 Datatypes -
    String, Number, Bigint, Boolean, Undefined, Null, Symbol, Object

  20. JavaScript has dynamic types. This means that the same variable can be used to hold different data types. All JavaScript numbers are stored in a 64-bit floating-point format. You can use the JavaScript typeof operator to find the type of a JavaScript variable. The typeof operator returns the type of a variable or an expression.

  21. In JavaScript, there are 6 falsy values -
    false, 0, "", null, undefined, NaN

Top comments (0)