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>
- getElementsByName
- getElementById
- getElementsByClassName
- All of the above
- define the content of webpages
- specify the layout of webpages
- program the behavior of webpages
- none of the above
- repeats an HTML element
- clones HTML elements once for each item in a collection
- used on an arry of objects
- defines initial values for an Angular JS application
- instanceof
- gettype
- typeof
- none of the above
- var
- let
- set
- None of these
- if
- for
- while
- switch
- HTML Conternt
- HTML Attribute Values
- HTML Styles
- AIl of the above
- exit
- break
- default
- out