JavaScript Variables

  • In JavaScript, the data is stored in the form of variables. Use  the following ways to declare JavaScript variables:

    • Automatically
    • Using var
    • Using let
    • Using const
    HTML JavaScript

    Always declare JavaScript variables before using them to avoid programming issues.

    JavaScript Variable

    When to Use?

    Automatically 

    Once used, values will ALWAYS be declared automatically. 

    var

    Use it ONLY when writing code for older browsers. 

    let

    ONLY use when const can’t be used

    const

    Used when values should be kept the same

    JavaScript Identifiers

    The identifiers are the unique names of JavaScript variables. 

    • It can be a short name or a letter like x, y, or z., etc.
    • Or it can be a full descriptive name, such as sum, height, age, etc. 

    JavaScript identifiers are not named randomly. The rules for naming JavaScript identifiers are:

    • Letters, numbers, underscores, and dollar signs can be used in an identifier’s name.
    • Should ALWAYS start with a letter or sign.
    • If the SAME letter is used in upper and lowercase, it will be two different identifiers.
    • No identifier should be named like JavaScript keywords. 

    The Assignment Operator

    JavaScript values are different from algebra. In JavaScript, the equal sign = is used to assign values. Therefore, an equal sign in JavaScript is always an assignment operator and never an equal to operator. It is written as a double equal sign “==”

    HTML JavaScript

    JavaScript Data Types

    A JavaScript variable value can be a number or a text. In programming languages, the text values are text strings. Here’s how data is written in JavaScript:

    • When writing numbers or digits, no quotation marks are used.
    • NEVER write text values without single or double quotes.
    • If numbers are written inside quotation marks, they will no longer be numbers and become a text string for JavaScript.
    HTML JavaScript

    Declaring a JavaScript Variable

    In programming, a variable is declared only if it is created. To declare these variables in JavaScript var and let keywords are used. For example, 

    • var subjectName or
    • let subjectName

    A variable with no assigned values is undefined. So, to assign the value to the variables mentioned above, we will use an equal sign.

    subjectName = “English”

    Or it can be written as var subjectName = “English” or let subjectName = “English”.

    The output value is inserted into the id= “demo” HTML paragraph after the variable is declared.

    HTML JavaScript

    Pro Tip: Always declare variables at the beginning of the code script.

    One Statement, Many Variables

    Declaring a single variable in one statement is not a compulsion. Many variables can be declared together in one statement. Use the let keyword to declare the variable and separate them all with commas in between.

    HTML JavaScript

    Value = undefined

    In programming, even if the value of a variable is not defined, it will still be declared. It will be considered a variable with an undefined value.

    HTML JavaScript

    Re-Declaring JavaScript Variables

    Variables can be re-declared without losing their value with the var keyword.

    However, variables declared with keywords “let” and “const” can not be re-declared.

    HTML JavaScript

    JavaScript Arithmetic

    Arithmetic statements in JavaScript are solved using the equal = and sum + operators.

    Numbers, text, and signs can all be used in JavaScript arithmetic.

    HTML JavaScript

    JavaScript Dollar Sign $

    In JavaScript, the dollar sign is a letter, not a sign. So, a valid variable name may contain a dollar sign. 

    Professional programmers usually use this sign when defining main functions in a JavaScript library.

    HTML JavaScript

    JavaScript Underscore (_)

    Like the dollar sign $, the underscore is also treated as a letter in JavaScript. Therefore, it can be used in JavaScript identifier names. Also, a variable name with an underscore is considered a valid name. 

    Professional programmers use an underscore for private or hidden variables.

    HTML JavaScript
  • HTML
    CSS
    JS
  • Ad #1
    Ad #2
    Scroll to Top