View video tutorial

JAVASCRIPT Variables

JAVASCRIPT

Variables in JavaScript can be considered as containers for data.

JavaScript variables


➔ JavaScript variables are used to store the value of any data or information.

➔ The variable should have a unique name within the scope of the variable called identifier.

JavaScript variables naming Conventions:


➔ Naming a variable requires following a standard naming convention for identifiers.

➔ The identifier name must start with a letter, underscore (_), or dollar sign ($).

➔ The characters following the first character of the identifier name can be any combination of numbers, letters, underscores, or dollar signs.

➔ Identifier names are case sensitive, so the uppercase and lowercase letters of the name are important.

JavaScript variable data type:


➔ JavaScript is a dynamically typed language, meaning that the data type of a variable is determined at runtime.

➔ Therefore, programmers do not have to specify the data type when declaring variables.

➔ A variable in JavaScript can hold information of the following data types, which are determined at runtime execution time.

Number: For integer and decimal data types.

String: For string or character data types.

Boolean: For logical values ​​like true or false.

Object: For object type data like arrays.

Null: Deliberately maintaining the absence of a value.

Undefined: The default value of a variable when it has no value assigned to it.

Keywords for Declaration in JavaScript


Keyword Scope Reassignment Redeclaration Description
let Block-scoped Allowed Not allowed The standard and recommended way to declare a variable whose value may change.
const Block-scoped Not allowed Not allowed The standard and recommended way to declare a constant type variable whose value cannot be changed after initialisation.
var Function-scoped Allowed Allowed (in non-strict mode) This is an outdated method of variable declaration and is not recommended in modern JavaScript due to potential scripting and extraction issues

Syntax

/* modern approach to declare a variable. */
let a = 20;
/* modern approach to declare a constant. */
const b = 30;
/* older method to declare a variable. not recommended */
var c = 10
JavaScript variables

Example

<!DOCTYPE html>
<html>
<head>
        <title>JavaScript variable Example</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta charset="utf-8">
</head>
<body>
        <h3>JavaScript variable Example</h3>
        <script>
                /* modern approach to declare a variable. */
                let a = 20;
                /* modern approach to declare a constant. */
                const b = 30;
                /* older method to declare a variable. not recommended */
                var c = 10
                console.log(a);
                console.log(b);
                console.log(c);
        </script>
</body>
</html>
Try it Now »

Click on the "Try it Now" button to see how it works.


JavaScript variable name

Example

<!DOCTYPE html>
<html>
<head>
        <title>JavaScript variable name example</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta charset="utf-8">
</head>
<body>
        <h3>JavaScript variable name example</h3>
        <script>
                /* The dentifier has underscore in its name. */
                let student_id = 50;
                /* The dentifier has dollar in its name. */
                let student$age = 22;
                /* The dentifier has underscore and digit in its name. */
                let st_address1 = "Road-05, House-06";
                console.log(student_id);
                console.log(student$age);
                console.log(st_address1);
        </script>
</body>
</html>
Try it Now »

Click on the "Try it Now" button to see how it works.


JavaScript dynamic variable type.

Example

<!DOCTYPE html>
<html>
<head>
        <title>JavaScript variable type example</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta charset="utf-8">
</head>
<body>
        <h3>JavaScript variable type example</h3>
        <script>
                /* variable number type */
                let id = 50;
                /* variable string type */
                let name = "Maria";
                /* variable boolear type */
                let eligible = true;
                /* variable undefined type */
                let email;
                /* variable null type */
                let address = null;
                /* variable array type */
                let skills = ["HTML", "CSS", "JS"];              
        </script>
</body>
</html>
Try it Now »

Click on the "Try it Now" button to see how it works.