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>



Qus. 1 : An element can be accessed in Java Script using:

  1. getElementsByName
  2. getElementById
  3. getElementsByClassName
  4. All of the above
Qus. 2 : What is the role of JavaScript in web design?

  1. define the content of webpages
  2. specify the layout of webpages
  3. program the behavior of webpages
  4. none of the above
Qus. 3 : What is not true about ng-repeat directive in Angular JS?

  1. repeats an HTML element
  2. clones HTML elements once for each item in a collection
  3. used on an arry of objects
  4. defines initial values for an Angular JS application
Qus. 4 : Which operator is used to get datatype of a variable in JavaScrip

  1. instanceof
  2. gettype
  3. typeof
  4. none of the above
Qus. 5 : Variables in JavaScript cannot be declared using:

  1. var
  2. let
  3. set
  4. None of these
Qus. 6 : Which JavaScript statement is used to perform different actions based on different conditions?

  1. if
  2. for
  3. while
  4. switch
Qus. 7 : JavaScript can change?

  1. HTML Conternt
  2. HTML Attribute Values
  3. HTML Styles
  4. AIl of the above
Qus. 8 : Which statement is used to come out switch block?

  1. exit
  2. break
  3. default
  4. out

Programs

Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad