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

</SCRIPT>


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


Attribute
Value
Description
Type
text/javascript
text/ecmascript
text/vbscript
the type of script
Language
Javascript vbscript
name of scripting language
Src
URL
a URL to a file that contains the script


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


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.

Syntax

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

Example

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

f_name

India123

_sumof

Some invalid variable names

10_numbers - must not begin with any number.

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

my name - Space is not allowed.


Example:

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



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