Quick Contact

    JavaScript Variables

    What is Variable?

    Variables are the foundation of all programming languages. Variables are used to store data such as strings of text, integers, and so on. The variables’ data or values could be set, modified, and retrieved as required. Variables, in general, are conceptual identifiers for values.

    The var keyword is used to create variables, but the assignment operator (=) is used to assign values to variables, as shown below:

    varvarName = value;

    Naming Conventions for JavaScript Variables

    There are several guidelines to follow when defining a JavaScript variable (also known as identifiers).

    The following are the guidelines for going to name a JavaScript variable:

    • The name of a variables could begin with a letter, underscore (_), or dollar symbol ($).
    • A variable name must not begin with a number.
    • A variable name could only consist alphanumeric letters (A-z, 0-9), underscores, and colons.
    • Spaces are not permitted in variable names.
    • A variable name must not contain a JavaScript keyword.


    Because variable names in JavaScript are case sensitive, $myvar and $myVar are two distinct variables. Be cautious while defining variable names.

    Example of JavaScript Variable

    < script>  
    var a = 50;  
    var b = 30;  
    var c=x+y;  
    < /script>  



    JavaScript local variable

    A local variable in JavaScript is defined within a block or function. It can only be available inside the function or block. For example:

    < script>  
    function abc(){  
    var x=10;//local variable  
    < /script>  

    JavaScript global variable

    A global variable in JavaScript is declared outside of the function or within the window object. It is accessible from any function.

    A global variable in JavaScript may be accessed from any function. A global variable is one that is defined outside of the function or declared with the window object. For example:

    < script>  
    var data=200;//gloabal variable  
    function a(){  
    function b(){  
    a();//calling JavaScript function  
    < /script>  

    Declaring Multiple Variables at Once

    Furthermore, you may define and initialize several variables in a single sentence. As seen in the following example, commas separate each variable:

    < html>
    < body>
    < script>
    // Declaring multiple Variables
    var name = "Amit", age = "25", MarriedorUnmarried= "Unmarried";
        // Printing variable values
    document.write(name + "< br>");
    document.write(age + "< br>");
    < /script>
    < /body>
    < /html>




    Q1. What are JavaScript variables?

    A JavaScript variable is nothing more than the name of a storage place. Variables in JavaScript are classified into two types: local variables and global variables. There are several guidelines to follow when declaring a JavaScript variable (also known as identifiers). The name must begin with a letter (a-z or A-Z), underscore (_), or dollar ($) symbol.

    Q2. What are the types of variables in JavaScript?

    Variables in JavaScript have just two scopes. Global variables have global scope, that means they may be declared anywhere in the JavaScript code. Local Variables– A local variable is only accessible within the function in which it is declared. Function parameters are always function-specific.

    Q3. How do you write variables in JavaScript?

    In JavaScript, use the let keyword to declare a variable. We may condense the variable declaration and assigning into a single phrase: if message = ‘Hello!’ / declare the variable and set its value to alert(message); / Hello!

    Q4. What is array in JavaScript?

    In JavaScript, an array is a single variable that stores several components. It is frequently used when we need to save the collection of elements and retrieve them using a single variable.

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.