DONATE  
MENU
JAVASCRIPT
AJAX
HTML
JSON

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Introduction » JavaScript Variables
Share Tweet WhatsApp

JavaScript Variables

A variable is a temporary container for storing values. Think of a variable as a place, where you can store a value and have access to it via its name in other parts of your program. EXAMPLE
6 //6 is a value but it was not stored
var myNum = 5; //5 is a value that has now been stored on myNum. When we use myNum, we are using its value (5)

/* myNum is now a variable containing a value
which can be use in further operations */
Variable Declaration To declare a variable in JavaScript, we make use of the var keyword followed by whatever name we shall reference/identify or call the variable with.
var x;
var myVar;
var person;
var num = 25;
var varName;
You can declare more than one variable on a single line and it will still be valid. Just ensure you separate each variable from the other with a comma (,).
var x, myVar, person, num = 25, varName;
Variable Assignment Variable assignment is when you pass or give a value of any data type to a declared variable to store. You may decide to assign values to a variable when it was declared/created or at a later moment when such a value is readily available to be assigned. A variable without a value is known as an undefined variable. EXAMPLE
var x; //Variable declared without a value assigned

var y = 5; //Variable declared with a value assigned

var z; //Variable declared without a value assigned

x = 6; //6 is now assigned to variable "x"

alert(x); //will alert 6
alert(y); //will alert 5
alert(z); //will alert undefined cause no value was assigned
<script>
var myNum = 5;
var you;
var me = "Omamode";

alert(myNum); //5
alert(you); //undefined
alert(me); //Omamode
</script>

Copy PRACTICE IT

You can change the value of a variable as many times as you want and the new value can be just any datatype.
var x = 2; //x is 2, a number

x = 8; //x is now 8, a number

x = "StackBeginner"; //x is now StackBeginner, a string
You can also assign values to variables declared on one line.
var i, d = 4, name = "Omamode", p;
/*variable i was not assigned
variable d was assigned 4
variable name was assigned Omamode
variable p was not assigned*/
Constant Variables A constant variable is a variable whose value never changes. During declaration, whatever value that is assigned to it can never be changed. To declare a constant variable, use the const keyword instead of the var.
<script>
const site_url = "http://www.stackbeginner.com";

site_url = "http://www.facebook.com"; //This cannot change the initial value

alert(site_url);

</script>

Copy PRACTICE IT

The above code may not work in some browsers because you are attempting to change the value of a constant variable and that is an error. Use a constant variable when the value of the variable should not change in the variable scope.
NOTE:
  • Assign a value to a constant variable during declaration else its value automatically becomes undefined, which can never be change.
    <script>
    const site_name;
    site_name = "StackBeginner"; //cannot change the "undefined" value already assigned to your variable
    
    alert(site_name);
    </script>

    Copy PRACTICE IT

JavaScript let Keyword The JavaScript let keyword allows you to temporary declare an already declared variable in a code block. A code block is a statement or group of statements contained in curly braces({ }). For example, loops and conditional statements. What the let keyword does exactly is that if a variable have already been declared and then, we want to use that variable in a code block without necessarily changing its previous value outside the code block, we therefore make use of the let keyword in redeclaring the variable inside the code block. Whatever value it is assigned inside the code block ends inside the code block.
var name = "Omamode";
//name is Omamode
{
let name = "Joseph";
//name will be Joseph inside this code block
}
//name is still Omamode outside the code block
<script>
var i = 50;

for (let i = 1; i < 2; i++){
alert("Inside the code block, i is "+i);
}

alert("Outside, i is originally "+i);
</script>

Copy PRACTICE IT

SUMMARY
  1. A variable is used in storing values. It is also a name given to a value which can be used in referencing that value whenever we need it.
  2. JavaScript variables are declared using the var keyword and then, the name of the variable.
  3. More than one variable can be declared in one JavaScript statement by separating each with a comma.
  4. The value a variable was created to store can be passed to the variable using the assignment operator (=). This can be done when the variable was declared or whenever the value is ready.
  5. A variable without a value is undefined.
  6. Constant variables are those whose values remain unchanged throughout the course of their existence. You can declare and immediately assign a value to a constant variable using the const keyword instead of the var keyword.
  7. The let keyword allows you to declare and use new values with an already declared variable without necessarily changing its value outside a code block.
Take a short quiz on JavaScript Variables.
Ask a question or tell us if JavaScript Variables is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode