css Unit 1
css Unit 1
Syllabus :
1.4 operators and expressions - primary expressions, object and array initializers, function
definition expression, property access expression, invocation expressions.
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
What is javascript ?
Javascript Is popular, lightweight And open source client side language supported by all
browser
It is scripting language beacause it can be used along with another languages.
It is not purely programming language and hence it doesn’t need compilation.
It is an interpreted programming language with object oriented capabilities.
Usually software developers use Programming languages like C, C++, Java On the other
hand, web developer use javascript.
1 To make web pages dynamic.
2 To respond to events.
3 To create interactive forms.
4 To validate data inputed by user.
5 To control browser data.
Java and javascript are two completely different languages in both concept and design.
However the syntax is similar.
Java powerful and much more complex programming language like C and C++ where ,
JS is not full-featured programming language rather, it is interpreted scripting language.
Java program needs compilation Whereas javascript program does not need compile.
Javascript only executed in browser
Java programs are executed in Java compatible application whereas, JS programs are
executed in all browsers.
Working of JS program :
1.1features of javacript
3.Event-Driven Programming
4.Dynamic Typing
JavaScript is dynamically typed, which means variable types are determined at runtime.
You don't need to specify the data type of a variable when you declare it.
5.Platform Independence
JS comes with a variety of built-in functions and methods for performing common tasks,
such as string manipulation, array operations, and mathematical computations.
The JavaScript ecosystem includes a vast number of libraries and frameworks (e.g.,
React, Angular, Vue.js, jQuery) that simplify and enhance web development.
JS seamlessly integrates with HTML and CSS, making it easy to create interactive web
pages. It can manipulate HTML elements and apply CSS styles dynamically.
How to write javascript?
<head>
<script>
//javascript code
</script>
</head>
OR
<body>
<script>
//javascript code
</script>
</body>
1 Language
2 Type
Language : specify scripting language of embeded code. The browser assumes JavaScript as the
default scripting language.
Type: the type attribute tells the browser that the script is in plain text and the text is organized
in the format of javascript. Default value is “text/javascript”.
Note : both attributes are optional and we should not use them.
</script>
Datatypes in JS :
A) Primitive
B) Non-primitive
A) primitive datatypes :
Name : “dev”,
Marks : 92
1. arithmetic
2. comparison
3. logical
4. bitwise
5. assignment
6. conditional/ternary
1. Arithmetic Operators :
Sr no Operator Meaning
1 + addition
2 - Substraction
3 * Multiplication
4 / division
5 % Modulus/remainder
<body>
<script>
let a = 10;
let b = 5;
</script>
</body>
Output :
Addition = 15
Substraction = 5
Multiplication = 50
Division = 2
Remainder = 0
2. Comparison Operators :
SR no Operator Meaning
1 < Less than
2 <= Less than or equal to
3 > Greater than
4 >= Greater than or equal to
5 == Exact equal to
6 != Not equal to
<body>
<script>
let a = 5;
let b = 10;
</script>
</body>
Output :
a < b = true
a <= b = true
a > b = false
a >= b = false
a == b = false
a != b = true
3. Logical Operators :
Sr no Operator Meaning
1 && Logical and
2 || Logical or
3 ! Logical not
<body>
<script>
let a = 5;
let b = 10;
document.write("<br>Logical AND : " + ((a < b) && (b > a)));
</script>
</body>
Output :
Logical OR : true
4. Bitwise Operators :
SR no Opertor Meaning
1 & Bitwise AND
2 | Bitwise OR
3 ^ Bitwise XOR
4 ~ Bitwise NOT
5 << Bitwise Left shift
6 >> Bitwise right shift
<body>
<script>
let a = 4;
</script>
</body>
Output :
Bitwise AND(5&3) : 1
Bitwise OR(5|3) : 7
Bitwise XOR(5^3) : 6
Bitwise NOT(~7) : -8
5. Assignment Operators :
SR no operator
1 +=
2 -=
3 *=
4 /=
<body>
<script>
let a = 5;
let b = 5;
a += b; //a = a + b
document.write("<br>" + a);
a -= b; //a = a - b
document.write("<br>" + a);
a *= b; //a = a * b
document.write("<br>" + a);
a /= b; //a = a / b
document.write("<br>" + a);
</script>
</body>
Output :
10
5
25
5
6. Conditional/ternary Operators :
i) ternary operator consist of condition if the condition is true then first(true) part
will execute and if condition is false then second(false) part will execute.
ii) syntax :
<body>
<script>
let a = 5;
let b = 10;
</script>
</body>
Output :
Greatest = 10
1.2 Object name, property, method, dot syntax, main event.
Define : An object in JavaScript is a collection of properties and methods. The object name is
the name you give to that collection.
let car = {
brand: "Toyota",
model: "Corolla"
};
1.2.2 Property:
Define :A property is a value that belongs to an object. It's like a variable that is tied to the
object.
let car = {
brand: "Toyota",
model: "Corolla"
};
Define :A method is a function that belongs to an object. It's a way to perform actions or
calculations based on the object’s properties.
let car = {
brand: "Toyota",
model: "Corolla",
start: function() {
};
Define : Dot syntax is how you access an object's properties or methods using a period (.)
between the object name and the property or method.
let car = {
brand: "Toyota",
model: "Corolla",
start: function() {
};
// Accessing a property
// Calling a method
Define : In JavaScript, an event is an action or occurrence detected by the browser, like a user
clicking a button or loading a webpage.
<body>
<script>
function showEvent() {
document.write("Button clicked!<br>");
function vinu(){
</script>
</body>
Note : A button is included with an onclick event that triggers the showEvent() function,
which writes "Button clicked!" to the webpage.
1.3values and variables
1.3.1 value:
Define: A value is any data that you can assign to a variable. It can be a number, string, boolean,
or any other data type in JavaScript.
1.3.2 Variables :
Define : A variable is a container that stores a value. You can create a variable, assign a value to
it, and then use or modify that value later in your code.
How to Declare Variables: In JavaScript, you can declare variables using var, let, or const.
<script>
let a = 10;
document.write(a);
</script>
Define : Primary expressions are the simplest, most basic expressions in JavaScript. They don't
involve any operators or complex syntax; they are just simple values.
Ex:
These are ways to create new objects or arrays with specific properties or elements.
Object initilizer :
Ex:
let car = {
brand: "Toyota",
model: "Corolla"
};
Note : Here, car is an object with brand and model as its properties.
Array initializer :
Ex :
<script>
let x = greet("Dev");
document.write(x);
</script>
<script>
document.write("fire caught!");
</script>
Define : Property access expressions are used to access the properties of an object. This can be
done using dot notation (.) or bracket notation ([]).
Ex :
<script>
let car = {
brand: "Toyota",
model: "Corolla"
};
</script>
Define : Invocation expressions are used to call (or invoke) functions. When you call a function,
you use an invocation expression.
Ex :
function greet(name) {
Note : Here, greet("John") is an invocation expression that calls the greet function with
the argument "John".
1.5 if statement, if…….else, if…….elseif, nested if statement.
1.5.1 If statement :
Define : if specified condition is true then code under if statement will be executed.
Ex:
<script>
</script>
Define : if specified condition is true then code of if block will be execute if specified condition
is false then code of else block will be execute.
Ex :
<script>
else {
</script>
Define : this statement allows to test multiple conditions.if condition1 is true then only block of
code under condition1 will be executed, if condition2 is true then only block of code under
condition2 will be executed, if none of the condition is true then block of code of else will be
executed.
Syntax :
if (condition1) {
} else if (condition2) {
} else {
Ex:
document.write("Grade: A+<br>");
document.write("Grade: A<br>");
} else if (marks >= 70) {
document.write("Grade: B<br>");
} else {
document.write("Grade: C<br>");
Output : Grade: A
Define : A nested if statement is an if statement inside another if statement. This allows you to
check a condition only if a previous condition is true.
Syntax :
if (condition1) {
if (condition2) {
Ex:
} else {
} else {
Define : The switch statement allows you to compare a variable against multiple values easily.
It is often clearer than using multiple if...else if statements, especially when you have many
cases to handle.
Ex:
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
Define : A for loop is used to run a block of code a specific number of times. It is often used
when you know in advance how many times you want to iterate.
Syntax :
Ex:
Output: 0 1 2 3 4
Define : A while loop continues to execute a block of code as long as a specified condition is
true. It is useful when you don’t know in advance how many times you want to loop
Syntax :
while (condition) {
// Code to run while the condition is true
Ex:
<script>
let i = 0;
while (i < 5) {
</script>
Output : 0 1 2 3 4
Define : A do...while loop is similar to a while loop, but it guarantees that the code block runs at
least once before checking the condition.
Syntax :
do {
} while (condition);
Ex:
num++;
Output : 10
Note : here condition is false still code under do block has run for once.
Define : The continue statement is used inside loops to skip the current iteration and move
on to the next one. It is useful when you want to skip certain values without breaking out of the
loop.
Ex:
if (i === 2) {
Output : 0 1 3 4
Define : The for...in loop is a powerful way to iterate over the properties of an object,
allowing you to access both keys and values easily.
Ex:
let car = {
brand: "Toyota",
model: "Corolla",
year: 2020,
color: "red"
};
Output :
brand: Toyota
model: Corolla
year: 2020
color: red
Execution explanation :
Object Creation:
The car object is created with four properties: brand, model, year, and color.
The for...in loop starts, and the property variable will hold the name of each property in the car
object during each iteration.
First Iteration:
Second Iteration:
Querying properties in JavaScript involves accessing the value of a property in an object. There
are two main ways to do this:
1.7.1.1dot notatation :
Syntax: object.property
Ex:
let person = {
name: "John",
age: 30
};
document.write(person.name);
Output: John
Syntax: object["property"]
Ex :
let person = {
name: "John",
age: 30
};
document.write(person["age"]);
Output: 30
Setting properties means adding a new property or modifying the value of an existing property
in an object.
let car = {
brand: "Toyota"
};
document.write(car.model);
Output: Corolla
Ex :
let car = {
brand: "Toyota",
model: "Corolla"
};
delete car.model; // Deletes the 'model' property
document.write(car.model);
Output: undefined
1.7.4 Getter Property :
A getter allows you to define a method that is called when you access a property.this function
has no any argument.
Syntax :
get propertyName() {
Ex:
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
get fullName() {
};
document.write(person.fullName);
A setter allows you to define a method that is called when you assign a value to a property.this
function have argument.
Syntax :
set propertyName(value) {
Ex:
let car = {
brand: "Toyota",
color: "red",
set company(value) {
this.brand = value;
};
Output :
Toyota red
Audi red