- JavaScript Basics
- JavaScript Home
- JavaScript Syntax
- JavaScript Placements
- JavaScript Output
- JavaScript Statements
- JavaScript Keywords
- JavaScript Comments
- JavaScript Variables
- JavaScript var
- JavaScript let
- JavaScript const
- JavaScript var Vs let Vs const
- JavaScript Operators
- JavaScript Arithmetic Operators
- JavaScript Assignment Operators
- JS Comparison Operators
- JavaScript Logical Operators
- JavaScript Bitwise Operators
- JavaScript Ternary Operator
- JavaScript Operator Precedence
- JavaScript Data Types
- JavaScript typeof Operator
- JS Conditional Statements
- JavaScript Conditional Statement
- JavaScript if Statement
- JavaScript if-else Statement
- JavaScript switch Statement
- JavaScript Loops
- JavaScript for Loop
- JavaScript while Loop
- JavaScript do-while Loop
- JavaScript Break Continue
- JavaScript Popup Boxes
- JavaScript Dialog Box
- JavaScript alert Box
- JavaScript confirm Box
- JavaScript prompt Box
- JavaScript Functions
- JavaScript Functions
- JS Function with Parameter
- JavaScript Return Statement
- JavaScript Variable Scope
- JavaScript setTimeout() Method
- JavaScript setInterval() Method
- JavaScript Events
- JavaScript Events
- JavaScript onclick Event
- JavaScript onload Event
- JavaScript Mouse Events
- JavaScript onreset Event
- JavaScript onsubmit Event
- JavaScript Objects
- JavaScript Objects
- JavaScript Number Object
- JavaScript Array Object
- JavaScript String Object
- JavaScript Boolean Object
- JavaScript Math Object
- JavaScript RegExp Object
- JavaScript Date Object
- JavaScript Browser Objects
- JavaScript Browser Objects
- JavaScript Window Object
- JavaScript Navigator Object
- JavaScript History Object
- JavaScript Screen Object
- JavaScript Location Object
- JavaScript Document Object
- JS Document Object Collection
- JS Document Object Properties
- JS Document Object Methods
- JS Document Object with Forms
- JavaScript DOM
- JavaScript DOM
- JavaScript DOM Nodes
- JavaScript DOM Levels
- JavaScript DOM Interfaces
- JavaScript Cookies
- JavaScript Cookies
- JavaScript Create/Delete Cookies
- JavaScript Advance
- JavaScript Regular Expression
- JavaScript Page Redirection
- JavaScript Form Validation
- JavaScript Validations
- JavaScript Error Handling
- JavaScript Exception Handling
- JavaScript try-catch throw finally
- JavaScript onerror Event
- JavaScript Multimedia
- JavaScript Animation
- JavaScript Image Map
- JavaScript Debugging
- JavaScript Browser Detection
- JavaScript Security
- JavaScript Misc
- JavaScript innerHTML
- JavaScript getElementById()
- JS getElementsByClassName()
- JS getElementsByName()
- JS getElementsByTagName()
- JavaScript querySelector()
- JavaScript querySelectorAll()
- JavaScript document.write()
- JavaScript console.log()
- JavaScript instanceof
- JavaScript Programs
- JavaScript Programs
- JavaScript Test
- JavaScript Online Test
- Give Online Test
- All Test List
JavaScript Variables
Variables in JavaScript are the containers for the data used in the program/application. For example:
<!DOCTYPE html> <html> <body> <p>Sum of <span id="one">10</span> and <span id="two">20</span> is <span id="res"></span></p> <script> var numOne = parseInt(document.getElementById("one").innerHTML); var numTwo = parseInt(document.getElementById("two").innerHTML); var result = numOne + numTwo; document.getElementById("res").innerHTML = result; </script> </body> </html>
Sum of 10 and 20 is
In above example, after initializing the value to variable say numOne, writing numOne anywhere in the program, means writing the value inside it or represents the value stored in it.
Types of Variables in JavaScript
In JavaScript, a variable can be declared using any of the following three keywords:
Depending on the use of keyword to declare a variable in JavaScript, there are three types of variables, that are:
- var Variable
- let Variable
- const Variable
Note - Use var to declare a global variable, that can be updated and re-declared.
Note - Use let to declare a block-scoped variable that can be updated, but can not be re-declared.
Note - Use const to declare a block-level variable that can neither be updated nor be re-declared. A const variable must be initialized with a value at the time of its declaration.
Rules to Name a Variable in JavaScript
- Variable name must starts with a letter (A-Z or a-z), a dollar sign ($), or an underscore (_)
- Use letters, digits (0-9), underscores, and dollar signs to name a variable
- Do not use any keyword to name a variable
- Variable names in JavaScript are case-sensitive. Therefore, num, Num, NUM are all different variables
Here are the list of some valid variable names example in JavaScript:
- num
- x
- _x
- $x
- first_name
- area34
- ar3t
- tr_$s_3_
« Previous Tutorial Next Tutorial »