Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

JavaScript Variables definition declaration with example

JavaScript Variables definition declaration with example

Variable definition – In programming, a variable is a value that can change, depending on conditions or on information passed to the program. In C Language variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. Its just like in Algebra,


Here X,Y and Z are variable , x value assigned to 5, y to 2 …. , All JavaScript variables must be identified with unique names called identifiers. Here x,y and z are identifier.

Commone Rules for creating names for variables (x,y,z ..) are :

  • Identifier Names can contain letters, digits, underscores, and dollar signs.
  • Identifier Names must begin with a letter (price,totalPrice,price2 …)
  • Identifier Names are case sensitive (x and X are different variables)
  • Reserved words (like JavaScript keywords) cannot be used as names
  • variable will declare with prefix var like var price

Example declaring variable

var price = 100; (valid)
var 2price= 100;(not valid)
var person = "Aayush";(valid)

We can also declare variable in single line like

var person = “Sanjay”, BikeName = “Bajaj Pulser”, price = 70000;

JavaScript Variables working Example

Create a page variable.html  copy and paste below code and save and run.

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Variable Example</title>
<input type="text" id="name" />    <br/>
<input type="submit" onclick="variableAlert()" value="Submit">
function variableAlert(){
var name = document.getElementById('name').value;

5 1 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments