CSS Unit 1-6
CSS Unit 1-6
■ The part of the web browser that understands HTML and CSS
is
called the layout or rendering engine. But most browsers also
have something called a JavaScript interpreter.
■ You can place the <script> tags, anywhere within your web page
(recommended to keep it within the <head> tags).
■ It is a kind of information
■ It is a kind of behaviour
■ For example,
– A submit button on login form will submit the form data to
server.
■ For example,
– A document object has a property bgColor and method write
■ For example, an event handler on flipkart website will enlarge image when user
mouse over the product
Build-in objects in JavaScript (Contd.)
■ Web browsers have an object named window.
■ Declaring a variable
– Use var keyword, which tells browser that text follow will be the variable
name
– Syntax:
var variable-name;
– Example
var color;
1.3 Variables
■ Initializing a variable
– Use assignment operator to assign value to variable
– Syntax:
variable-name = value;
– Example
■ Comparison operators
■ Logical operators
■ Bitwise operators
■ Assignment operators
■ Bitwise operators
1.4 Operators (Contd.)
■ Ternary operator
• – JavaScript includes special operator called
ternary operator : ? that
• assigns a value to a variable based on some
condition.
• – This is like short form of if-else condition.
• Syntax-
<condition>? <Value1>:<Value2>?
1.4 Expressions (Contd.)
■ When one or more operators are combined with one or
more
operands then it is called an expression.
• ■ For example:
• // This function returns the square of the value passed to it.
• var square = function(x) { return x * x; }
1.4 Expressions Property Access Expressions
• ■ Evaluates the value of an object property or an array element.
• expression . identifier
• expression [ expression ]
• ■ For example,
• var obj={x:1, y:2}
• obj.x //1
• obj.y //2
• var arr= [1,2,3]
• arr[2] //3
1.4 Expressions Invocation Expressions
• An invocation expression is JavaScript’s syntax for calling (or executing) a
• function or method.
• ■ Syntax:
• if (conditional expression)
• {
• // code block statements here.
• }
• else
• {
• // code block statements here.
• }
1.5 if...else statement
1.5 if...else if statement (Contd.)
• ■ The if...else if statement tells the browser, “If the condition is true, then execute
• these statements, else evaluate another condition. If the other condition is true,
• then execute these other statements.
• ■ Syntax:
• if (conditional expression)
• {
• //Place statements here.
• }
• else if (conditional expression)
• {
• //Place statements here.
• }
• ..s
• else
• {
• }
1.5 if...else if statement
1.6 switch...case statement (Contd.)
• ■ A switch...case statement tells the browser
to compare a switch
• value with a series of case values.
• case value2:
• //Place statements here.
• break;
• default:
• //Place statements here.
• }
1.7 Looping Statement
• ■ You can also control how a browser makes a decision by using a
• loop.
continue;
1.8 Querying and Setting properties
(Contd..)
■ To obtain (or query) the value of a object property
1) Use dot syntax
While using dot operator, the RHS must be identifier
For example
var author = book.author;
var name = student.surname;
■ For example
book.edition = 5;
book[“main_title”] = “ECMAScript”;
1.8 Deleting properties
■ The delete operator is used to remove the
property from an object.
■ For example
delete book.author; // The object now has no
author property
delete book[“main_title”]; // The object now
has no main_title property
1.8 Getters and Setters (Contd.)
■ There are two kinds of object properties.
1) Data properties – These have a simple value
2) Accessor properties. They are essentially
functions that execute on getting and setting a
value