CSS Chapter 1 Part 1
CSS Chapter 1 Part 1
Basics of JavaScript
CHAPTER 1 Programming
Syllabus
1.1 Features of JavaScript
1.2 Object Name, Property, method, Dot syntax, main event.
1.3 Values and Variables
1.4 Operators and Expressions- Primary Expressions, Object and
Array initializers, function definition expression, property access
expressions, invocation expressions.
1.5 IfStatement, if... else,if..elseif, nested if statement.
1.6 Switch ..case statement
1.7 Loop statement - for loop, for...in loop, while loop, do..while loop,
continue statement.
1.8 Querying and setting properties and deleting properties, property
getters and setters.
Introduction toJavaScript
GQ. Whatis JavaScript?
GQ Enlist different uses of JavaScript.
GQ.State the need ofJava Script.
GQ How to write JavaScript program?
GQ. Howto print data in JayaScript?
C
JavaScript is a dynamic computer programming language that executes
On the browser. It modifies static HTML web pages into interactive web
Pages by dynamically updating content, validating form data, controlling
multimedia, animate images on the web pages.
Scripting Language (MSBTE)
ClientItSide
is a scripting language for creating dynamic web page content.
improvingsite visitors' interaction with
for
creates elements menus, animated graphics, and dynamic web page,
such as dropdown
colors. background,
Itis lightweight and most commonly used as a part of web pages, whoe
implementations allow client-side script to interact with the user and
Use
Editor
<title>...</itle>
<s(Newcript>document.write(Hello, World!');</seript>
Syll. w.ef
academic year 19-20) (D5-16A) ITech-Neo Publications
Client Side Scripting Language (MSBTE)
(Basics of JavaScript Prog.) Pg. no.
</head>
(1-5)
<body>... S/body>
</html>
4. External JavaScript
Third way JavaScript can be adding
the src attribute. externally using <script> tag with
The src attribute specify the path of
external JavaScript file.
Example (External File)
JavaScript
Pile:messagejs
function msg
alert(Hello, World!y
The alert() method displays an alert box over the current window with
the
specified message.
Example
alert("Hello, World!");
3. Using console.log()
The console.log() is used in debugging the code and print the data in browser
console.
Example
console.log(HelloWorld');
1.1 FEATURES OF JAVASCRIPT
UQ. List any four features of Javascript. MSBTE - Q. 1(a), W-19, 2 Marks
GQ. State any four features of JavaScript.
JavaScript is an object-based scripting language.
Giving the user more control over the browser.
It Handling dates and time.
It Detecting the user's browser and OS,
It is light weighted.
Client - Side Technology
JavaScript is a scripting language and it is not java.
JavaScript is interpreter based scripting language.
JavaScriptis case sensitive.
JavaScript is object based language as it provides predefined objects.
Every statement in JavaScript must be terminated with semicolon (;
Syntax of
Most of the JavaScript control statements Syntax is same as
control statements in C 1anguage.
Tech-Neo Publication
(New Syll. w.e.f academic year 19-20) (D5-16A)
Cient Side Scripting Language (MSBTE) (Basics of JavaScript Prog.) Pg. no. (1-7)
An important part of Javascript is the ability to create new functions
withinscripts. Declare a function in JavaScript using function keyword.
N1.2 OBJECT NAME, PROPERTY, METHOD, DOT
SYNTAX, MAIN EVENT
Go. Describe the different ways of creating the object using JavaScript.
GO, Explain the term object with property in JavaScript with exarmple.
GO. Explain the term object with method/function in JavaScript with
example.
GQ Explain howto accessing object propertiesin JavaScript.
GQ. Howto add methodto a JavaScript Object ?
GQ. State the use of this keyword in JavaScript
GO. Explain the terms main event in lavaScript with example.
a 1.2.1 Object
AJavaScript object is a collection of named values.
JavaScript objects are containers for named values, called properties and
methods.
There are different ways to create new objects:
1. Create a single object, using an object literal.
2. Creating instance of Object directly, with the keyword new
3. Define an object constructor, and then create objects of the
constructed type.
4. Create an object using Object.create()
1. JavaScript Object by object literal
An object literal is a list of name:value pairs inside curly braces {}.
Using an object literal,define and create an object in one statement.
Syntax
object= Dertyl:valuel, property2:value2... propertyN:valueN}
OR
object= {keyl:valuel, key2:value2 ... keyN:valueN}
(New Syll. w.e.f academic year 19-20)(D5-16A) Gech
Tech-Neo Publications
(Basics of JavaScript
Prog.)
ClientSide
Scripting
Language
(MSBTE)
Pg.no (4
Example </head>
</title >
<html> Example
<title>Object
<head>
<body> "text/javaScript"> "Somwanshi", age:25}:
<scripttype = {fname:"Ravi",Iname:
=
const person {
person)
document.write(person[x] + "<br/>")
for (letx in
</script>
</body
</html>
Output
Ravi
Somwanshi
25 Object
instance of
2. By creating new Object() and then ad:
object using
Create a new JavaScript
properties.
Syntax
var objectName new Obect}S
Example
<htmi>
<head><itle> Object Example</itle> </head
<body>
<seript type text/javaScript
const person new Object0;
person.fname-"Rayi!,
person.Iname="Somwanshi
person.age=25; person.age
document. write(person.fname +""4 Person.Jname + "is " +
years old.");
</script>
</body>
</html>
Tech-NeoPublca
(NewSyll. w.e.f academic year 19-20) (D5-16A)
Client Side Scripting Language (MSBTE)
(Basics of JavaScript Prog.) Pg. no.
(1-9)
Output
Ravi Somwanshi is 25 years old.
2 By using an Object constructor
We need to create function with
be assign in the current object by arguments
using this
and each argument value can
The this keyword refers to the current
keyword.
object.
Example
<html>
<head><title> Object Example</itle></head
<body>
<seript type="textjavaScript">
function emp(id, name, salary){
thisad=id;
this.name=name;
this.salary =salary;
e=new emp(103,"Prashant Somwanshi",30000);
document.write(e.id +" "+e.name +""+e.salary);
</seript>
</body>
<{html>
Output
103 Prashant Somwanshi 30000
4. Usingthe Object.create() method
Objects can also be created using the Object.create() method.
t allows choosing the prototype object for the object we want to create,
without having to define a constructor function.
Example
<html>
<head> <itle> Object Exanple</útle> </head>
<body>
<script type =
Const person "text/javaScript">
(New Syl.. w.e.f academic yyear 19-20) (D5-16A) Tech-Neo Publications
(Basics of JavaScript Prog.) Pg.rno.
Language (MSBTE)
ClientSide
Scripting (1-10)
properties
fname:"Ravi",
Default value of
Iname:"Somwanshi",
age:25,
function as a value
/ using
print:function()
document.write(this.fname "<br/>");
fname:"Ravi".
Iname:"Somwanshi",
(New Syll. w.e.f
academic year 19-20) (D5-16A) Tech.NeoPublico
Client Side Scripting Language (MSBTE) (Basicsof JavaScript Prog.) Pg. no. (1-11)
age: 25
};
Here, fname: "Ravi", Iname : "Somwanshi" and age: 25 are properties.
A 1.2.3 Method
Methods are actions that can be performed on objects.
An object method is an object property containing a function definition.
Methods are functions stored as object properties.
Example
Const person =
fnarme : "Ravi',
Iname : "Somwanshi".
age : 25.
Wusing function as a value
print: function)
{
console.log(hello);
}
l accessing method
person.print(); // hello
NOte: In above example, function is use as a value for the print key. So, we
heed to use person.print() instead of person.print to call the function inside
the object.
Adding aMethod to a JavaScript Object
We can also add a method in an object.
Example
creating an empty object
let student = ;
lladding a
student.nameproperty ;
=Ravi'
adding amethod
/ accessing a method
student.print(); // hello
"this" Keyword
To access a property of an object from within a method of the same object
we need to use this keyword.
Example
const person
name: Ravi',
age: 25,
l accessing nane property by using this.name
print: function()
person.print);
Output
This name is Raví
properu
In the above example, a person object is created. It contains
(name and age) and a method print.
object,this
In the method print, while accessing a property of an
keyword is used.
LaTech-NeoPublicati"
(NewSyll. w.e.f academic year 19-20) (D5-16A)
Client Side Scripting Language (MSBTE) (Basics of JavaScript Prog.) Pg. no. (1-13)
1. Using dot Notation
We can access the value of aproperty by using (.) dot notation.
Syntax
objectName.key
Example
let person =
fname : "Ravi
Iname : "Somwanshi",
age : 25
l accessing property
console.log(person.fname): // Ravi
2. Using bracket Notation
objectNamef"expression"|
Example
let person
fname :"Ravi",
Iname :"Somwanshi",
age : 25
X= person.age}
accessing property by propertyName
aTech-Neo Publications
(New Syll. w.e.f academic year 19-20) (D5-16A)
(Basics of JavaScript
Client Side Scripting Language (MSBTE) Prog.) Pg.
console.log(person["Sname"); / Ravi ,no.(1 4
/accessing property by expression
console.log(person[x); / 25
1.2.5 Main Event
The change in the state of an object is known as an Event.
In html, there are various events which represents that Some
performed by the user or by the browser. activity is
When JavaScript code is included in HTML,. JavaScript reacts Over
these
events and allows the execution. This process of reacting over the even.
is called Event Handling. Thus, JavaScript handles the HTML evente .:
Event Handlers.
place.
Keyboard events
The keyboard events that are invoke by keyboard.
Event Performed Event Handlert Description
Keydown & Keyup Onkeydown &onkeyup When the user press
and then release the key
IS Formevents
The form events that are invoke by form.
document.write("This is a JavaScript");
}
</script>
<form>
<input type="button" onclick="lickevent)" value="Click Me"
</form>
</body>
<{html
Output
(Before Click)
Javascript Events
File D:/TechNeo%20Publicationsievent.htmi
Cick Me
(After Click)
event.htnl
O FileD:/TechNeo%20Publications/event.htmi
This is a JavaScript
I Declaring a variable
To declare a variable, we use the let, var and const keyword followed by
the variable name.
Undefined is also a primitive value in JavaScript. Undefined means
unknown value.
Syntax
let <variable-name>;
Example
variable declaration andUndefined
let myName; /
let myAge;
S Initializing a variable
value.
declared, we need to initialize it with a
Once variable is
variable.
equal sign is used to assign value to
An
Syntax
value>
let <variable-name>
Example
let myName "Ravi", variable declaration and initialization
myAge = 25; l variable initialization
Constants
Example
const myBirthday =18.04.1995
S Variable types / Data types
1, Numbers
We can store numbers in variables, either whole numbers like 30(alsocalled
floating
integers) or decimal numbers like 2.456 (also called floats or
point numbers).
Example
let myAge = 17;
2. Strings
Strings are pieces of text. When we give a variable a string value, we need to
wrap it in single or double quote marks.
Example
let myNarme "Ravi";
3. Booleans
1. Arithmetic Operators
The arithmetic operators are used to perform arithmetic operations on the
operands.
Operator Description Example
Addition 10+20= 30
Subtraction 20-10 = 10
3. Bitwise Operators
The bitwise operators perform bitwise operations on operands.
Operator Description Example
Bitwise AND (10==20 & 20==33) = false
| Bitwise OR (10==20120==33) =false
<<
Bitwise Left Shift (10<<2)= 40
Bitwise Right Shift (10>>2) =2
>>> Bitwise Right Shift with Zero (10>>>2) = 2
4. Logical Operators
6. Special Operators
The following are the special operators in JavaScript:
Operator Description
(?:) Conditional Operator returns value based on the condition.
It is like if-else.
The this' evaluates to the current objiect. It is use in a method to refer to the
current object
Example
this.name
this.displayName)
a 1.4.2 Object and Array Initializers
1, Object initializer expression
Object initializer creates object with literal notation and the value of this
expression is newly created object.
It uses curly brackets surrounding object properties separated by
Commnas.
Example
var obj =
propl: "value]".
prop2: 2
}:
2. Array nitlallzer expresslon
Array initializer creates array with literal notation and the
value of this
expression is newlycreated array.
Example
var arr = [1,2, 3);
Syntax
object.property lireference a property (or method) of an object
object{property]
object[property]
Example
var obj = (x: 1, y: 2}:
obj.x /
objl'y] 2
var arr = 2,3;
arr[l] II 3