0% found this document useful (0 votes)
3 views

unit 4

Uploaded by

vikashgrg06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

unit 4

Uploaded by

vikashgrg06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

JavaScript Basics

Lovely Professional University


JavaScr
ipt
• For JavaScript, you should know the basic language i.e., HTML and
CSS.
• JavaScript is the programming language of HTML and the web. It
makes web page dynamic. It is interpretated programming language
with object oriented capabilities.
Basic Structure

• Document.write() function is used to display the data on the webpage.


Linking more than one
javascript file.
Write function() -

Used to concatenate
the words.
Alert function in JavaScript
JavaScript Variable

A JavaScript variable is simply a name of storage location. There are two types of variables in
JavaScript : local variable and global variable.

There are some rules while declaring a JavaScript variable (also known as identifiers).

1.Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.


2.After first letter we can use digits (0 to 9), for example value1.
3.JavaScript variables are case sensitive, for example x and X are different variables.
Rules for defining the variables-
Javascript Data Types

JavaScript provides different data types to hold different types of values. There are two types of data
types in JavaScript.
1.Primitive data type
2.Non-primitive (reference) data type
JavaScript is a dynamic type language, means you don't need to specify type of the variable
because it is dynamically used by JavaScript engine. You need to use var here to specify the data
type. It can hold any type of values such as numbers, strings etc.

For example:
3.var a=40;//holding number
4.var b="Rahul";//holding string
JavaScript non-primitive data types

The non-primitive data types are as follows:


For Loop
For loop example-
JavaScript Operators
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while, do while or for-in
loops. It makes the code compact. It is mostly used in array.
There are four types of loops in JavaScript.
1.for loop
2.while loop
3.do-while loop
4.for-in loop
JavaScript Array
JavaScript array is an object that represents a collection of similar type of elements.
There are 3 ways to construct array in JavaScript
1.By array literal
2.By creating instance of Array directly (using new keyword)
3.By using an Array constructor (using new keyword)

1) JavaScript array literal


The syntax of creating array using array literal is given below:
JavaScript array constructor (new keyword)

Here, you need to create instance of array by passing arguments in constructor so that we don't
have to provide value explicitly.
The example of creating object by array constructor is given below.

The example of creating object by array


constructor is given below.
1.<script>
2.var emp=new Array("Jai","Vijay","Smith");
3.for (i=0;i<emp.length;i++){
4.document.write(emp[i] + "<br>");
5.}
6.</script>

Output of the above example


Jai
Vijay
Smith

You might also like