Variables in Java Script

Getting started with javascript

JavaScript is not a full-fledged language and it needs to be embedded within a HTML document. Otherwise, or to specify an external file that contains the JavaScript code we can use word ‘script’ or ‘program’ for the JavaScript code.

The most common way to set off a script is to use the HTML <script> and </script> tags in HTML document. We can place our JavaScript code in either the HEAD or BODY section of a HTML document.

The Syntax (General format) is

<SCRIPT [Attributes = [“Value”] …. ]>           Indicates starting of JavaScript Code

… JavaScript statement(s);                            Indicates ending of JavaScript Code


The following table contains Script attributes, values and their description.

the type of script
Javascript vbscript
name of scripting language
a URL to a file that contains the script

<TITLE>My First JavaScript program
<SCRIPT type=”text/javascript”>
document.write(“Welcome to JavaScript Programming!”);

Variable in Java Script 

A variable is an identifier that can store values. These values can be changed during the execution of script. Once a value is stored in a variable it can be accessed using the variable name. Variable declaration is not compulsory, though it is a good practice to use variable declaration. Keyword var is used to declare a variable.


var var-name [= value] [..., var-name [= value] ]


var name = “Sachin”; // Here ‘name’ is variable
document.write(name); // Prints Sachin
A JavaScript variable can hold a value of any data type. For example :
i = 7;
document.write(i); // prints 7
i = “seven”; // JavaScript allows to assign string values
document.write(i); // prints seven
Some valid examples of variable declaration:
var cost;
var num, cust_no = 0;
var amount = 2000;

Naming Conventions

We should use meaningful name for a variable. A variable name must start with a letter, underscore (_), or dollar sign ($). The subsequent characters can be the digits (0-9). JavaScript is case sensitive, so the variable name my_school is not the same as My_School.

Some valid variable names




Some invalid variable names

10_numbers - must not begin with any number.

rate% - ‘%’ is not a valid character.

my name - Space is not allowed.


<TITLE>Sum of two numbers</TITLE>
<SCRIPT type=”text/javascript”>
var a = 100;
var b = 500;
var c = a + b;
document.write (“Sum of a & b : “ + c );


java script code to show the reservation system of XYZ

View Solution

java Script Application for Student attendance

View Solution

java script function max to find largest number

View Solution

JavaScript code that calculates the squares and cubes from 0 to 10

View Solution

JavaScript code to design a simple calculator to perform the following operations sum product difference and quotient

View Solution

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad Sarkari Exam Quiz