Close Menu
    Facebook X (Twitter) Instagram
    Oixiesoft
    • Home
    • Services
      • WordPress Malware Removal
      • Fix WordPress Errors
      • WordPress Website Development
    • Articles
    • Contact
    Oixiesoft
    Home»Tutorial»JS»JS Variables
    JS

    JS Variables

    Editorial StaffBy Editorial StaffUpdated:May 14, 2022No Comments3 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    JavaScript Variables

    Learn about naming conventions and data types and how to declare and assign values to JavaScript variables.

    There are four 4 Ways to Declare a JavaScript Variable:

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

    What are Variables?

    JavaScript variables store data. Therefore you can apply them to temporarily save the values your program needs to operate. Think of them as little containers or boxes that hold a specific piece of data that you specify.

    Example

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>JavaScript Variables Example</h2>
    
    <p> a, b, and c are variables.</p>
    
    <p id="test"></p>
    
    <script>
    var a = 7;
    var b = 10;
    var c = a + b;
    document.getElementById("test").innerHTML =
    "The value of c is: " + c;
    </script>
    
    </body>
    </html>

    In the above example, a, b, and c, are three variables and declared these variables with the var keyword.

    Declaring variables

    To create a variable you must declare it with the var keyword. The var keyword tells the JavaScript interpreter to create a new variable. Let’s create a variable named a, b and c.

    Example

    
    var a 
    var b
    
    

    In the above example, a and b are two variables and declared these variables with the var keyword.

    The Assignment Operator

    After you declare a variable with var, you need to assign a value, until you do the variable has a value equal to undefined.
    In practical terms, it means that the variable has no value assigned to it.
    To assign a value to a variable use the equal sign =

    Example

    a = 7;
    b = 8;
    

    Notice how you didn’t use the var keyword here. You already declared the variable in the code above. Here you are assigning a value to an existing variable.

    Declaring and assigning a value at the same time

    If you are declaring a variable for the first time and know the value that should be assigned to it. You can use a one-line declaration and assignment statement:

    Example

    var a = 7;
    var b = 8;
    var c = a + b;
    

    In the above example, a, b, and c, are three variables and declared these variables with the var keyword.

    JavaScript Identifiers

    All JS variables must be specified with unique names.

    These unique names are contacted identifiers.

    You must follow certain rules when declaring (naming) variables in JavaScript. JavaScript variable names must adhere to all the following rules:

    • Must begin with a letter, dollar sign $ or underscore _.
    • Can contain letters, digits, and the special characters $ and _ only.
    • There are certain reserved words that you cannot use, like var for example.
    • Remember that JavaScript is a case-sensitive language, this also applies to variables. Car isn’t the same as car.

    When declaring variables try to use descriptive names. The variable name x is not descriptive at all, but the name carHasAirbag, tells you what kind of data this variable may have.

    Data types

    JavaScript variables can contain numbers( like 200) and text values ( like “Mary Doe”) also.

    JavaScript can handle many different data types. A data type is a group of data with values having predefined characteristics.

    Strings are composed inside double or single quotes. Digits are composed without quotes. As an example, a string data type can contain alphanumeric characters.

    
    <!DOCTYPE html>
    <html>
    <body>
    <h2>JavaScript Data types</h2>
    <p>Strings and Numbers data type expamples</p>
    <p id="test"></p>
    <script>
    const pi = 3.14;
    let human = "Mary Doe";
    let reply = 'Yes I am!';
    
    document.getElementById("test").innerHTML =
    pi + "<br>" + human + "<br>" + reply;
    </script>
    
    </body>
    </html>
    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Tumblr Email
    Editorial Staff

    Related Posts

    CodeIgniter vs Laravel: A Detailed Side-by-Side Comparison

    Python Arrays

    Python Lambda

    Services
    • Web Development
    • Mobile Application Development
    • WordPress Malware Removal Service
    • Website Design
    • WordPress Development
    • Magento Development
    • Shopify Development
    • SEO Services
    Blog
    • How to Fix the Error Establishing a Database Connection
    • Ping List WordPress
    • How To Fix Japanese Keyword Hack
    • How to remove Malware from WordPress
    Hire Developers
    • Hire WordPress Developer
    • Hire Shopify Developer
    Contact Info
    • Oixiesoft Technologies
      A-40, Block A, I thum Tower, Sector 62, Noida
    • sales@oixiesoft.com
    • Privacy Policy
    • About Us
    • Contact Us
    © 2025 OixieSoft Technologies

    Type above and press Enter to search. Press Esc to cancel.