UNIT - Ibbhhhhhhhhhhhbhhhhhhhhhhbbbbbbbbbb
UNIT - Ibbhhhhhhhhhhhbhhhhhhhhhhbbbbbbbbbb
Web development refers to the creating, building, and maintaining of websites. It includes
aspects such as web design, web publishing, web programming, and database management. It
is the creation of an application that works over the internet i.e. websites. Web development
is typically broken down into 3 core areas: Frontend, Backend, Full Stack Development.
Frontend Development
The Face of the Web. The part of a website where the user interacts directly is termed as front
end. This involves designing the structure, layout, and behavior of the website It is also
referred to as the ‘client side’ of the application.
Frontend Technologies
• HTML: HTML stands for HyperText Markup Language. It is used to design the front
end portion of web pages using markup language. It acts as a skeleton for a website
since it is used to make the structure of a website.
• CSS: Cascading Style Sheets fondly referred to as CSS is a simply designed language
intended to simplify the process of making web pages presentable. It is used to style
our website.
• Bootstrap: Bootstrap is a free and open-source tool collection for creating responsive
websites and web applications. It is the most popular CSS framework for developing
responsive, mobile-first websites. Nowadays, the websites are perfect for all browsers
(IE, Firefox, and Chrome) and for all sizes of screens (Desktop, Tablets, Phablets, and
Phones).
Frontend Frameworks/Libraries
• React.js : A popular JavaScript library for building dynamic, component-based user
interfaces.
• Vue.js : A progressive JavaScript framework that is flexible and can be used for
building both simple and complex user interfaces.
Backend Development
The Backbone of the Web. Backend is the server side of a website. It is part of the website
that users cannot see and interact with. It is the portion of software that does not come in
direct contact with the users. It is used to store and arrange data.
Server-side Programming Languages and Frameworks
• Java: Java is one of the most popular and widely used programming languages. It is
highly scalable.
• Python: Python is a programming language that lets you work quickly and integrate
systems more efficiently.
Node.js Express
C# .NET
Databases
• MySQL
• PostgreSQL
• MongoDB
• MariaDB
• SQLite
• RESTful API's
• GraphQL
Databases
1. Relational Databases
A relational database stores data in tables, similar to a spreadsheet, where each table has rows
and columns. The rows hold individual records, and the columns define the data attributes.
Tables can be linked to each other through special keys, allowing related data to be
connected.
2. NoSQL Databases
A NoSQL database stores data in a flexible, non-tabular format, unlike traditional relational
databases. Instead of using tables with rows and columns, NoSQL databases might use
documents, key-value pairs, wide-columns, or graphs to store data. This allows them to
handle large amounts of unstructured or semi-structured data efficiently. They are designed to
scale easily and manage big data applications.
Front-End Development:
• Proficiency in HTML, CSS, and JavaScript.
• Experience with front-end frameworks and libraries such as React, Angular, or Vue.js.
Back-End Development:
Database Management:
• Skill in writing queries and ensuring proper data storage and retrieval.
Server Environment:
• Knowledge of server setup, configuration, and management.
Version Control/Git:
• Proficiency in using version control systems, particularly Git, for code management
and collaboration.
• Experience working with APIs to facilitate communication between the front end and
back end or integrating third-party services.
• Ability to deploy applications to servers or cloud platforms and manage the hosting
environment.
Full-stack developers are valued for their versatility and the ability to contribute to different
stages of the development process. They play a crucial role in small to medium-sized
development teams and startups where having individuals with a broad skill set can be
particularly advantageous. However, it's important to note that as technology evolves, the
definition of a "full-stack" developer may change, and the specific technologies and skills
required may vary based on the industry and project requirements.
• Full-stack developers can work on both the front-end and back-end of a project,
providing a versatile skill set. This versatility allows them to contribute to various
aspects of the development process.
End-to-End Understanding:
Effective Communication:
• Full-stack developers can communicate more effectively between front-end and back-
end teams, as they have experience working on both sides. This can lead to better
collaboration and a smoother development process.
Faster Development:
• With the ability to work on both front-end and back-end tasks, full-stack developers
can streamline the development process. This can lead to faster iteration cycles and
quicker delivery of features.
Adaptability to Technology Changes:
Reduced Dependencies:
Cost-Efficiency:
• In certain scenarios, having a smaller team of full-stack developers may be more cost-
effective than maintaining separate front-end and back-end teams. This is especially
true for smaller projects or startups with budget constraints.
Holistic Problem Solving:
While the full-stack approach offers these advantages, it's important to note that
specialization in specific areas of development is also valuable. The choice between full-stack
and specialized roles depends on project requirements, team structure, and the goals of the
organization.
HTML:
HTML stands for Hyper Text Markup Language. It is the standard language used to create
and structure content on the web.
• It forms the building blocks of all websites and is complemented by CSS for style and
JavaScript for interactivity.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Welcome to My Webpage</h1>
</body>
</html>
1. Build Websites: HTML is the basic building block for creating any website. Learning
HTML can help you pursue a career in web development.
2. Customize Content: Allows you to edit or tweak web pages, emails, or templates to
fit your needs.
3. Understand how the web works: Helps you grasp how the internet works and how
web pages are structured.
4. Employment Opportunities: According to Bureau of Labor Statistics projects that
employment for web developers will grow 16% between 2022-2032, which is much
faster than the average across all occupations.
5. Learn Easily: HTML is beginner-friendly, making it a great first step into the world
of coding and technology
Features of HTML
• It is a markup language.
HTML Element and HTML Tags are related but distinct. An HTML element is the complete
structure, including the opening tag, content (if any), and the closing tag (if applicable).
On the other hand, A tag is the actual keyword or name enclosed in angle brackets (< >) that
tells the browser what kind of content to expect.
Example:
HTML Page Structure
The basic structure of an HTML page is shown below. It contains the essential building-block
elements (i.e. doctype declaration, HTML, head, title, and body elements) upon which all
web pages are created.
• <!DOCTYPE html> – This is the document type declaration, not a tag. It declares
that the document is an HTML5 document.
• <html> – This is called the HTML root element. All other elements are contained
within it.
• <head> – The head tag contains the “behind the scenes” elements for a webpage.
Elements within the head aren’t visible on the front end of a webpage. Typical
elements inside the <head> include:
• <title> – The title is what is displayed on the top of your browser when you visit a
website and contains the title of the webpage that you are viewing.
• <body> – The body tag is used to enclose all the visible content of a webpage. In
other words, the body content is what the browser will show on the front end.
Note:
▪ Character Setting: Specifies how text characters are encoded and displayed, often
using <meta charset="UTF-8">.
▪ Viewpoint (Viewport): Defines how a page's layout and content adapt to different
screen sizes and resolutions, set with <meta name="viewport
Example Program
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Viewport Example</title>
</head>
<body>
<h1>Responsive Design</h1>
<p>This page adjusts to your device's screen size.</p>
</body>
</html>
History of HTML
HTML (Hyper Text Markup Language) is the standard language used to create web pages. It
has evolved significantly since its inception, incorporating features to meet the needs of
modern web development. Below is a brief history of HTML:
1. Early Development
• 1989: Tim Berners-Lee, a physicist at CERN, proposed the idea of a global hypertext
system, which led to the development of HTML.
• 1991: The first version of HTML was introduced, containing only basic tags like <p>,
<a>, <img>, and <h1>.
• A stricter and cleaner version of HTML 4.01, following XML syntax rules.
6. HTML5 (2014)
While HTML5 remains the base standard, updates are continuously made by WHATWG
(Web Hypertext Application Technology Working Group). HTML is now treated as a "Living
Standard," meaning it evolves continuously without major version numbers.
Key Features of HTML5 (and beyond):
1. Semantic Elements:
2. Multimedia Support:
4. Form Enhancements:
5. Geolocation API:
7. Web Storage:
o Replaces cookies with localStorage and sessionStorage for storing data on the
client side.
8. Improved Accessibility:
o ARIA (Accessible Rich Internet Applications) roles integrate better support for
screen readers.
Future of HTML
The web community continues to enhance HTML, focusing on features such as:
• Support for advanced technologies like Web Components and Virtual Reality (VR).
5 key uses of HTML are:
• Creating web page.
• Semantic markup
• Cross-platform compatibility
Advantages of HTML
3. Cross-Platform Compatibility
o HTML works on all major platforms and browsers, ensuring wide usability.
o HTML can be combined with other languages like CSS and JavaScript to
create dynamic and visually appealing websites.
6. Wide Range of Tools and Resources
o With modern HTML5, developers can easily create responsive and mobile-
friendly websites using the <meta viewport> tag and other features.
Disadvantages of HTML
1. Static Nature
o HTML alone is static and cannot create dynamic, interactive web pages
without the help of JavaScript or server-side technologies.
2. Limited Styling Options
o While HTML supports basic formatting, complex designs require CSS for
proper styling.
o Writing HTML code for large projects can become verbose (expressed in more
words than needed)and repetitive without tools like templating engines.
o While modern browsers support HTML5 features, older browsers may not
fully support them, requiring fallbacks or polyfills.
6. Security Risks
o Improper HTML usage, such as failing to escape input, can lead to security
vulnerabilities like Cross-Site Scripting (XSS).
8. Maintenance Challenges
In HTML, block-level and inline elements are ways to structure web pages. Block-level
elements start on new lines and take up the full width of the page. Inline elements do not start
on new lines and only take up the space needed by their content.
1. Block-Level Elements
• Display Behavior: These elements start on a new line and take up the full width
available (by default), stretching to the left and right as far as they can.
• Common Examples:
o <div>
o <p>
o <h1> to <h6>
o <ul>, <ol>, <li>
• Use Case: Used to structure the layout of a webpage or define distinct sections.
2. Inline Elements
• Display Behavior: These elements do not start on a new line and only take up as
much width as necessary. They flow along with the text or content around them.
• Common Examples:
o <span>
o <a>
o <img>
o <label>
• Use Case: Used to style or format parts of content within a block of text or to insert
smaller elements.
Key Differences
Content Can contain block or inline elements Only contain inline elements
Example program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
.block-element {
background-color: lightblue;
padding: 10px;
margin: 10px 0;
.inline-element {
background-color: lightcoral;
padding: 5px;
</style>
</head>
<body>
<p>
Inline elements can also be inside block-level elements, like <strong class="inline-
element">this strong tag</strong> within this paragraph.
</p>
</body>
</html>
Output:
_
Semantic tags in HTML are elements that clearly describe their meaning to both the browser
and the developer. These tags provide better structure, readability, and accessibility to a
webpage. Here's an overview of commonly used semantic tags with examples:
1.<header>
<header>
<h1>Welcome to My Website</h1>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
2.<footer>
Represents the footer of a document or section, typically containing copyright info, links, or
contact details.
<footer>
<nav>
</footer>
3.<article>
Represents self-contained content, such as a blog post or news article.
<article>
</article>
4.<section>
Defines a thematic grouping of content, typically with a heading.
<section>
<h2>Features</h2>
<p>Our product offers exceptional performance, scalability, and reliability.</p>
</section>
5.<nav>
<nav>
<ul>
<li><a href="#services">Services</a></li>
<li><a href="#portfolio">Portfolio</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
6<aside>
Represents content related to the main content, such as a sidebar or callout.
<aside>
<h3>Did You Know?</h3>
</aside>
7.<main>
Represents the main content of a document, excluding repeated elements like headers or
footers.
8.<main>
<h1>About Our Company</h1>
</main>
10.<time>
Represents a specific time or date.
_____________________________________________
Sample Program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
line-height: 1.6;
}
header, footer {
background-color: #333;
color: white;
text-align: center;
padding: 1em 0;
}
nav ul {
list-style: none;
padding: 0;
nav ul li {
display: inline;
margin: 0 10px;
}
nav ul li a {
color: white;
text-decoration: none;
main {
display: flex;
padding: 20px;
}
aside {
width: 25%;
padding: 10px;
background: #f4f4f4;
margin-right: 20px;
}
article {
width: 75%;
figure {
text-align: center;
margin: 20px 0;
}
footer nav {
margin-top: 10px;
</style>
</head>
<body>
<header>
<h1>Welcome to My Blog</h1>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#articles">Articles</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
<main>
<aside>
<h2>About Me</h2>
<p>Hi, I'm a web developer who loves creating semantic and accessible
websites.</p>
<h3>Quick Links</h3>
<ul>
</ul>
</aside>
<article>
<section>
<h2>Featured Article</h2>
<p>In this article, we discuss the importance of <mark>semantic HTML</mark> in
modern web development.</p>
<figure>
</figure>
</section>
</article>
</main>
<footer>
<p>© 2025 My Blog. All rights reserved.</p>
<nav>
</nav>
</footer>
</body>
</html>
Output:
__________________________________________________________________________
The <form> tag in HTML is used to create a form for collecting user input. It serves as a
container for various input elements such as text fields, checkboxes, radio buttons, submit
buttons, and more. Forms are primarily used to send data to a server for processing.
1.action: Specifies the URL to which the form data will be sent.
2.method: Defines the HTTP method to be used when submitting the form (GET or POST).
3.enctype: Specifies how the form data should be encoded when submitting it to the server
(e.g., application/x-www-form-urlencoded or multipart/form-data).
Sample Program:
Output
The enctype attribute specifies how the form-data should be encoded when submitting it to
the server.
Syntax:
<element-name attribute-name="attribute-value">Content</element-name>
element-name: The name of the HTML element (e.g., <a>, <img>, <div>).
The following are the rules and characteristics of HTML attributes; you should follow while
using attributes with HTML elements:
1.Attributes are optional; you can use them to provide additional information about an HTML
element.
2.Attributes have name and value pairs, but some of the attributes do not require any value;
those are known as Boolean attributes.
3.An HTML element can have multiple attributes, and they should be separated by spaces.
4.Attributes should always be written with the opening tag.
5.All HTML elements can have attributes except a few like <head>, <title>, <script>, etc.
6.W3C recommends using attributes in lowercase and keeping the value in quotes.
Styling with CSS
What is CSS?
CSS stands for Cascading style sheets. It describes to the user how to display HTML
elements on the screen in a proper format. CSS is the language that is used to style HTML
documents. In simple words, cascading style sheets are a language used to simplify the
process of making a webpage.
CSS is used to handle some parts of the webpage. With the help of CSS, we can control the
colour of text and style of fonts, and we can control the spacing between the paragraph and
many more things. CSS is easy to understand but provides strong control on the Html
documents.CSS is combined with HTML.
Advantages of CSS
o Faster page speed: It has a faster page speed than other code's page speeds. With the
help of the CSS rule, we can apply it to all occurrences of certain tags in HTML
documents.
o Better user experience: CSS makes a webpage very attractive to the eyes. Also, CSS
makes it user-friendly. When the button or text is in a proper format, it improves the
user experience.
o Quicker Development time: With the help of CSS, we can specify the format and
style the multiple pages into one code string. In cascading style sheet, we can make a
duplicate copy of several website pages.
If we make a webpage, it has the same formatting, looks, and feel, so with the help of
the CSS rule for one page, and it is sufficient for all the pages.
Using CSS
CSS can be added to HTML documents in 3 ways:
Inline CSS
An inline CSS is used to apply a unique style to a single HTML element.
An inline CSS uses the style attribute of an HTML element.
The following example sets the text color of the <h1> element to blue, and the text color of
the <p> element to red:
<!DOCTYPE html>
<html>
<body>
Internal CSS
An internal CSS is used to define a style for a single HTML page.
An internal CSS is defined in the <head> section of an HTML page, within
a <style> element.
The following example sets the text color of ALL the <h1> elements (on that page) to blue,
and the text color of ALL the <p> elements to red. In addition, the page will be displayed
with a "powderblue" background color:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output:
External CSS
An external style sheet is used to define the style for many HTML pages.
To use an external style sheet, add a link to it in the <head> section of each HTML page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output:
CSS Selectors
CSS provides various types of selectors to apply styles to HTML elements. Below are the
most commonly used selectors along with examples:
4. ID Selector (#)
• Targets a single element with a unique ID.
css
CopyEdit
#main-header {
font-size: 24px;
}
html
CopyEdit
<h1 id="main-header">Welcome to My Website</h1>
5. Group Selector (,)
• Applies the same styles to multiple elements.
css
CopyEdit
h1, h2, h3 {
font-family: Arial, sans-serif;
}
/* Element Selector */
h1 {
color: blue;
}
/* ID Selector */
#main-content {
background-color: lightgray;
padding: 10px;
}
/* Class Selector */
.highlight {
background-color: yellow;
}
/* Group Selector */
h2, h3 {
color: darkgreen;
}
/* Descendant Selector */
div p {
color: red;
}
/* Child Selector */
div > span {
color: purple;
}
/* Attribute Selector */
input[type="text"] {
border: 2px solid black;
}
/* Pseudo-Class Selector */
a:hover {
color: orange;
}
/* Pseudo-Element Selector */
p::first-letter {
font-size: 24px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Main Heading</h1>
<h2>Subheading</h2>
<p>This paragraph is bold because of the adjacent sibling selector.</p>
<p>This paragraph is underlined because of the general sibling selector.</p>
<div id="main-content">
<p>This paragraph inside div is red.</p>
<span>This span inside div is purple.</span>
</div>
The CSS Box Model is a fundamental concept in web design and development. It defines
how the elements on a web page are structured and how space is distributed around
elements. Understanding the Box Model is crucial for controlling layout and spacing in your
web pages.
The Box Model describes the rectangular boxes generated for elements in the document
tree. Each element in HTML is represented as a box, and this box consists of several
components that define its size, padding, borders, and margins.
1. Content:
o This is the actual content of the box, such as text, images, or other HTML
elements.
o The size of the content area is determined by the width and height
properties.
o Example: If you set width: 200px; and height: 100px;, the content area will be
200px by 100px.
2. Padding:
o Padding is the space between the content and the border. It creates space
inside the box, around the content.
o Padding is transparent and doesn't have any color unless you set the
background color on the box.
o You can define different padding for each side (top, right, bottom, left) using:
o Example:
padding: 10px;
3. Border:
o The border is a line surrounding the padding (if any). It is placed between the
padding and margin.
o Borders can be styled with width, color, and style (solid, dashed, dotted, etc.).
o Example:
o You can also control borders individually for each side (top, right, bottom,
left):
4. Margin:
o Margin is the outermost space around the element. It is the space between
the element's border and its neighboring elements.
o Margins are transparent and are used to create distance between elements.
o Example:
margin: 20px;
Box Model Visualization
+----------------------------+
| Margin |
| +------------------------+ |
| | Border | |
| | +------------------+ | |
| | | Padding | | |
| | | +------------+ | | |
| | | | Content | | | |
| | | +------------+ | | |
| | +------------------+ | |
| +------------------------+ |
+----------------------------+
1. Content: The inner part where the actual content like text or images are placed.
3. Border: Surrounds the padding (if present) and separates it from the margin.
4. Margin: The outermost space, creating distance between this element and others.
The total width and height of an element are calculated by adding the content width,
padding, border, and margin.
• Total Width:
Total Width = content width + left padding + right padding + left border + right border + left
margin + right margin
• Total Height:
Total Height = content height + top padding + bottom padding + top border + bottom border
+ top margin + bottom margin
Box Model Example
Here’s an example of how the Box Model is applied to an HTML element with CSS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.box {
width: 200px;
height: 100px;
padding: 20px;
margin: 30px;
background-color: lightblue;
</style>
</head>
<body>
</body>
</html>
• Padding adds 20px on each side (making the content area effectively 240px wide).
• Total Width: 200px (content) + 20px (left padding) + 20px (right padding) + 5px (left
border) + 5px (right border) + 30px (left margin) + 30px (right margin) = 310px
Box-Sizing Property
The box-sizing property allows you to control how the width and height of an element are
calculated.
• Default: The default box-sizing value is content-box, meaning the width and height
only apply to the content box, and padding/border are added outside of this.
• Alternative: box-sizing: border-box; includes padding and borders inside the width
and height calculation.
Example:
.box {
box-sizing: border-box;
width: 200px;
height: 100px;
padding: 20px;
With box-sizing: border-box, the total width remains 200px, including the padding and
border.
• JavaScript is also used on the Server side (on Web Servers) to access databases, file
handling and security features to send responses, to browsers.
2. Client Side: JavaScript is the main language for client-side logic and is supported by
almost all browsers. There is a big list of frameworks and libraries like React
JS, Angular JS, and Vue JS.
JavaScript is a versatile, dynamically typed programming language used for interactive web
applications, supporting both client-side and server-side development, and integrating
seamlessly with HTML, CSS, and a rich standard library.
• JavaScript is a single-threaded language that executes one task at a time.
• The data type of the variable is decided at run-time in JavaScript that’s why it is
called dynamically typed.
A “Hello, World!” program is the simplest way to get started with any programming
language. Here’s how you can write one using JavaScript.
<html>
<head></head>
<body>
<scrip t>
console.log("Hello, World!");
</script>
</body>
</html>
In this example
• console.log() prints messages to the browser’s developer console. Open the browser
console to see the “Hello, World!” message.
*/
In this example
o Single-line Comment: The // syntax is used for short, inline comments, like
the one explaining the console.log function.
• Versatile: JavaScript can be used for a wide range of tasks, from simple calculations
to complex server-side applications.
• Asynchronous: JavaScript can handle tasks like fetching data from servers without
freezing the user interface.
• Rich Ecosystem: There are numerous libraries and frameworks built on JavaScript,
such as React, Angular, and Vue.js, which make development faster and more
efficient.
• Declarative Programming: Focuses on what should be done rather than how it’s
done. It emphasizes describing the desired result, like with arrow functions, without
detailing the steps to achieve it.
Applications of JavaScript
• Server Applications: Node.js brings JavaScript to the server side, enabling powerful
server applications and full-stack development.
• Game Development: JavaScript, combined with HTML5 and libraries like Ease JS,
enables the creation of interactive games for the web.
Limitations of JavaScript
• Security Risks : JavaScript can be used for attacks like Cross-Site Scripting (XSS),
where malicious scripts are injected into a website to steal data by exploiting elements
like <img>, <object>, or <script> tags.
• Performance : JavaScript is slower than traditional languages for complex tasks, but
for simple tasks in a browser, performance is usually not a major issue.
• Weak Error Handling and Type Checking : JavaScript is weakly typed, meaning
variables don’t require explicit types. This can lead to issues as type checking is not
strictly enforced.
Variables and data types are foundational concepts in programming, serving as the building
blocks for storing and manipulating information within a program. In JavaScript, getting a
good grasp of these concepts is important for writing code that works well and is easy to
understand.
Variables
A variable is like a container that holds data that can be reused or updated later in the
program. In JavaScript, variables are declared using the keywords var, let, or const.
1. var Keyword
var n = 5;
console.log(n);
console.log(n);
Output
20
The let keyword is introduced in ES6, has block scope and cannot be re-declared in the same
scope.
let n= 10;
console.log(n)
Data Types
JavaScript supports various datatypes, which can be broadly categorized into primitive and
non-primitive types.
Primitive Datatypes
let n = 42;
let pi = 3.14;
4. Undefined: A variable that has been declared but not assigned a value.
let notAssigned;
console.log(notAssigned);
Output
undefined
6. Symbol: Represents unique and immutable values, often used as object keys.
Non-Primitive Datatypes
Non-primitive types are objects and can store collections of data or more complex entities.
let obj = {
name: "Amit",
age: 25
};
function fun() {
console.log("GeeksforGeeks");
}
JavaScript Operators
JavaScript operators are symbols or keywords used to perform operations on values and
variables. They are the building blocks of JavaScript expressions and can manipulate data in
various ways.
const p = 4 * 2; // Multiplication
const q = 8 / 2; // Division
Output
8884
Assignment operators are used to assign values to variables. They can also perform
operations like addition or multiplication before assigning the value.
let n = 10;
n += 5;
n *= 2;
console.log(n);
Output
30
Comparison operators compare two values and return a boolean (true or false). They are
useful for making decisions in conditional statements.
Output
true
false
Comparison operators are mainly used to perform the logical operations that determine the
equality or difference between the values.
Output
false
true
console.log(res);
Output
The ternary operator is a shorthand for conditional statements. It takes three operands.
console.log(status);
Output
Adult
Comma Operator (,) mainly evaluates its operands from left to right sequentially and returns
the value of the rightmost operand.
let n1, n2
const res = (n1 = 1, n2 = 2, n1 + n2);
console.log(res);
Output
let x = 5;
console.log(++x); // Pre-increment
Output
632
JavaScript Relational operators are used to compare its operands and determine the
relationship between them. They return a Boolean value (true or false) based on the
comparison result.
console.log("length" in obj);
console.log([] instanceof Array);
Output
true
true
BigInt operators allow calculations with numbers beyond the safe integer range.
console.log(big1 + big2);
Output
1111111110111111111011111111100n
JavaScript String Operators include concatenation (+) and concatenation assignment (+=),
used to join strings or combine strings with other data types.
console.log(s);
Output
Hello World
• + concatenates strings.
The optional chaining operator allows safe access to deeply nested properties without
throwing errors if the property doesn’t exist.
console.log(obj.address?.city);
console.log(obj.contact?.phone);
Output
Delhi
undefined
JavaScript operators are special symbols used to perform operations on operands, which can
be values or variables. They allow you to manipulate data and perform computations.
• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• Logical Operators
• Bitwise Operators
• String Operators
• Conditional (Ternary) Operator
• Type Operators
• Comma Operator
• Unary Operators
Control flow statements in JavaScript control the order in which code is executed. These
statements allow you to make decisions, repeat tasks, and jump between parts of a program
based on specific conditions.
1. Decision-Making Statements
JavaScript if Statement
Output
The if...else statement provides an alternate block of code to execute if the condition is false.
console.log("You passed.");
} else {
console.log("You failed.");
Output
You failed.
The if...else if...else statement is used when you want to handle multiple conditions.
console.log("It's hot.");
console.log("It's warm.");
} else {
console.log("It's cold.");
Output
It's warm.
2. Loops
The for loop continuously execute a block of code to a set number of times.
console.log(i);
Output
The while loop repeats a block of code as long as the condition is true.
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
Output
The do...while loop executes the block of code at least once before checking the condition.
let i = 1;
do {
console.log(i);
i++;
Output
3. Branching Statements
The return statement is used to end the execution of a function and optionally return a value
to the function caller.
function add(a, b) {
return a + b;
console.log(res);
Output
In this example
• return n1 + n2 ends the function and sends the sum of a and b back to the caller.
if (i === 3) break;
console.log(i);
Output
1
The continue statement skips the current iteration of a loop and proceeds to the next one.
if (i === 3) continue;
console.log(i);
Output
4. Switching Statements
The switch statement evaluates an expression and executes a block of code based on
matching cases. It provides an alternative to long if-else chain.
switch (day) {
case "Monday":
console.log("Start of the week.");
break;
case "Friday":
break;
default:
Output
Java Functions:
A named function is a function that has a specific name and can be called anywhere
in the script. It is defined using the function keyword followed by a name.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
function displayMessage() {
function addNumbers(a, b) {
let sum = a + b;
</script>
</head>
<body>
<h2>JavaScript Named Function Example</h2>
<p id="output"></p>
<p id="result"></p>
</body>
</html>
Output:
No function name.
Usually assigned to variables.
Can be used as callbacks (e.g., in setTimeout or event listeners).
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
};
let product = a * b;
};
</script>
</head>
<body>
<h2>JavaScript Anonymous Function Example</h2>
<p id="output"></p>
<p id="result"></p>
</body>
</html>
Output:
3. Arrow Function - A shorter syntax introduced in ES6.[ ES6 is the 6th version of the
ECMA Script programming language. ECMA Script is the standardized name for
JavaScript. ECMA stands for European Computer Manufacturers Association]
Advantages of Arrow Functions:
Shorter syntax.
No need for the function keyword.
return is implicit if the function has a single expression.
Does not bind its own this (useful in object methods and callbacks).
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
};
let sum = a + b;
};
// Arrow function with implicit return (single expression)
</script>
</head>
<body>
<p id="output"></p>
<p id="result"></p>
<p id="product"></p>
</body>
</html>
Output:
4. Immediately Invoked Function Expression (IIFE) - A function that runs as soon as
it is defined.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p id="message"></p>
<p id="greeting"></p>
<p id="sum"></p>
<script>
window.onload = function() {
(function() {
})();
(function(name) {
})("John");
(() => {
})();
// IIFE with a return value
return a + b;
})(10, 20);
};
</script>
</body>
</html>
Output:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
// Named Function
function showMessage() {
function greetUser(name) {
// Arrow Function
document.body.style.backgroundColor = "lightblue";
};
</script>
</head>
<body>
</body>
</html>
5-Mark Questions
15. Write a basic JavaScript function to validate if a text input field is empty.
16-Mark Questions
These require detailed explanations, code examples, and diagrams (where applicable).
2. Discuss the importance of semantic HTML elements. Write examples for <header>,
<footer>, <article>, and <section>.
3. Describe HTML forms in detail. Include examples of input elements like text, radio
buttons, checkboxes, and submit buttons.
4. Explain the various types of CSS selectors (e.g., class, id, group, descendant) with
examples.
5. Discuss the CSS box model in detail. Explain how padding, border, and margin affect
layout.
6. Explain the differences between flexbox and grid layout in CSS with examples. Write
a sample code to design a simple web page layout using both.
7. Explain data types and variables in JavaScript with examples. Highlight the
differences between primitive and non-primitive data types.
8. Write a detailed note on control structures in JavaScript (if, switch, for, while) with
examples.
9. Explain the role of functions in JavaScript. Write a program to calculate the factorial
of a number using a function.
10. Create a JavaScript program to validate a registration form. Include checks for name,
email, password, and phone number fields.
1. HTML + CSS: Create a simple web page with a header, a navigation bar, and a form
using HTML. Style it using CSS to create a visually appealing layout. (16 Marks)
2. HTML + JavaScript: Design an HTML form for user login. Write JavaScript code to
validate the username and password fields. (16 Marks)
3. CSS + JavaScript: Explain how JavaScript can manipulate CSS styles dynamically.
Write a program to change the background color of a <div> when a button is clicked.
(16 Marks)