Saturday, May 20, 2023

JavaScript Variables

 

JavaScript Variables


4 Ways to Declare a JavaScript Variable:

  • Using var
  • Using let
  • Using const
  • Using nothing  

    What are Variables?

    Variables are containers for storing data (storing data values).

    In this example, xy, and z, are variables, declared with the var keyword:  

    Redeclaring a Variable Using let 

    Redeclaring a variable inside a block will not redeclare the variable outside the block:

  •  Redeclaring a variable using the let keyword can solve this problem.

    When to use JavaScript const?

    Always declare a variable with const when you know that the value should not be changed.

    Use const when you declare:

    • A new Array
    • A new Object
    • A new Function
    • A new RegExp  

      Constant Objects and Arrays

      The keyword const is a little misleading.

      It does not define a constant value. It defines a constant reference to a value.

      Because of this you can NOT:

      • Reassign a constant value
      • Reassign a constant array
      • Reassign a constant object

        But you CAN:

      • Change the elements of constant array
      • Change the properties of constant object

      Constant Arrays

      You can change the elements of a constant array:

No comments:

Post a Comment

JavaScript Animated

  JavaScript Animated <!DOCTYPE html> <html> <style> #myContainer {   width: 400px;   height: 400px;   position: relative;...