Skip to main content

Variable life cycle in JavaScript | JavaScript Variables Lifecycle

JavaScript Variables Lifecycle

When the JavaScript engine works with variables, their life-cycle consists of these 3-steps:

  1. Declaration phase - create a new variable, E.g. var myValue;

  2. Assignment phase - Assigning a value to the Variable in JavaScript, E.g. myValue = 150;

  3. Usage phase - Access and use the variable value, E.g. alert(myValue);

1.) Variable Declaration :

  • In any programming language creating a variable is called “declaring” a variable.

  • Declaring a variable means the computer reserves memory in which it will store the variable data. The program can then read/write data in this memory area by manipulating the variable.

Let's see an examples of variable declarations:

 ➤ Syntax : Single variable

    Declare Single variable in a line

<script>
     var <var_name>;
</script>
Parameter :
  • var: JavaScript reserved keyword.

  • var_name: Name of variable.

 ➤ Example : Declare Single variable in a line

<script>
     var carName;
</script>

You can also declare many variables in one statement(a Single Line).

 ➤ Syntax : Declare multiple variables in a single-line

    Start the statement with var and separate the variables by comma.

<script>
     var <var_name1>,<var_name2>,<var_name3>;
</script>

 ➤ Example :

<script>
     var carName,carNumber,carColor;
</script>

Declare multiple variables in a multiple-line.

 ➤ Syntax : Multiple variable in multiple-line;

    Start the statement with var and separate the variables by comma.

<script>
     var <var_name1>,
        <var_name2>,
        <var_name3>;
</script>

 ➤ Example :

<script>
     var carName,
        carNumber,
        carColor;
</script>

Important point's of variable declaration :

  • In JavaScript, traditional way to declare a variable with the var keyword. We need to declare variables before we use them, otherwise we receive an ReferenceError.

  •  ➤ Syntax : Use variable without declare;

    <script>
        //Uncaught ReferenceError: myVariable is not defined
         document.write(myVariable);
    </script>
  • When a variable has been declared it exists in memory but it is has no value. In JavaScript this is represented as undefined.

  •  ➤ Syntax : Use variable before assiging value;

    <script>
         var myVariable;
         document.write(myVariable); //undefined
    </script>


2.) Variable Assignment :

  • Storing a value in a variable is called variable Assignment/initialization.

  • While a program is running, the value stored in a variable if you want can change variable value. To give new value to a variable, use the assignment operator "=".

Let's see an examples of variable Initialization :

 ➤ Syntax :

    To assign a value to the variable, using the assignment operator "=".

<script>
     var <var_name>;
     <var_name> = <value>;
</script>

Note : A value can be assigned to a variable by using the equals(=) sign, which is called assignment operator(=).

Parameter :
  • var: JavaScript reserved keyword.

  • var_name: Name of variable.

  • value: Optional, Initial value assign at the time of declaration or not.

 ➤ Example :

    Now, we can put some data into variable by using the assignment(=) operator;

<script>
     var carName;
     carName =  "Audi" // Store the string;
</script>

You can assign a value to the variable either while you declaring the variable or after declaring the variable(when you need that variable).

Initialization of Javascript Variable in a single line.

  • The declaration and assignment can be combined into a single-statement, When a variable is assigned a value it then becomes defined.

  •  ➤ Syntax : Single variable in a line;

    <script>
         var <var_name>= <value>;
    </script>

     ➤ Example :

    <script>
         var carName="Audi"; // Store the string
    </script>

Initialize multiple variables in a Single Line.

  • There is a shorthand way of creating multiple variables in the same statement by using the comma-operator(,).

  •  ➤ Syntax : Multiple variable in a line;

        Start the statement with var and separate the variables by comma.

    <script>
         var <var_name1>= <value>, <var_name2>=<value>, <var_name3>=<value>;
    </script>

     ➤ Example :

    <script>
         var carName="Audi",carNumber=1234,carColor="Black";
    </script>

Initialize multiple variables in multiple-line.

 ➤ Syntax : Multiple variable in multiple line;

    Start the statement with var and separate the variables by comma.

<script>
     var <var_name1>= <value>,
        <var_name2>=  <value>,
        <var_name3>= <value>;
</script>

 ➤ Example :

<script>
     var carName="Audi",
        carNumber=1234,
        carColor="Black";
</script>


3.) Variable Usage :

  • Access the value of variable is called Usage of variable;

  • The process usually goes this way : 1'st a variable should be declared, then initialized with a value and finally used.

Let's see an examples of variable Usage:

 ➤ Syntax :

<script>
     var <var_name>= <value>,
        document.write(<var_name>);  //shows the variable content
</script>

 ➤ Example :

<script>
     var carName="Audi",
        document.write(carName);
</script>
Output :
Audi

Comments

Popular Posts

How to remove the date and .html from every blogger post url

#Remove date and .html from blogger post url A Common search term which every blogger search is How to Remove Date From Blogger Post URL or how do I remove date from blogger permalink? Follow the steps below and then date and .html will be removed from the URL of your blogger post. Step 1 : Login to your Blogger blog and select Theme / Template. Step 2 : Click on Edit HTML and paste the below code just above the </head> tag let's see code :   ➤ Code : mycode.js; Copy code <script type='text/javascript' > //<![CDATA[ // BloggerJS v0.3.1 var urlTotal,nextPageToken,postsDatePrefix=!1,accessOnly=!1,useApiV3=!1,apiKey="",blogId="",postsOrPages=["pages","posts"],jsonIndex=1,secondRequest=!0,feedPriority=0,amp="&"[0];function urlVal(){var e=window.location.pathname,t=e.length;return...

Django static files not working when debug false || debug true

# Django static and media files not working when debug is false In this article you will learn how to fix problem of not loading static files and media in Django even the DEBUG is FALSE. This is the easiest and safest solution. # Problem: Django static and media files not working when debug is false  ➤ Code: settings.py DEBUG = False #True ALLOWED_HOSTS = [ '*' ] #Host name # Problem Fix: Let's see, How you can fix the problem of Django static and media files not working when DEBUB = False : 1.)First way: devserver in insecure mode If you still need to server static locally ( e.g. for testing without debug ) you can run devserver in insecure mode: python manage.py runserver --insecure --insecure: it means you can run serve...

AssertionError: SessionMiddleware Must Be Installed

  Fixing the "AssertionError: SessionMiddleware Must Be Installed" in FastAPI This error typically occurs when you try to access  request.session  in custom middleware before the  SessionMiddleware  has been properly applied. Here's how you can resolve this issue and ensure smooth session handling in your FastAPI application. Understanding the Issue The error indicates that the  SessionMiddleware  is either not installed correctly or is being accessed too late in the middleware stack. To resolve this, you need to make sure that  SessionMiddleware  is added to the FastAPI application before any custom middleware that relies on session data. Error Example Code: Here's a simple FastAPI application that demonstrates the error setup for session middleware: from fastapi import FastAPI, Request from starlette.middleware.sessions import SessionMiddleware app = FastAPI() # Add SessionMiddleware first app.add_middleware(SessionMiddleware, secret_key = ...