Introduction to JavaScript
JavaScript is a powerful, versatile programming language primarily used to
add interactivity and dynamic behavior to websites. Unlike HTML and CSS, which
structure and style content, JavaScript lets you manipulate webpage elements,
respond to user actions, and even communicate with servers.
It runs directly in the browser, making it an
essential part of modern web development.
Role of JavaScript in Web Development (HTML +
CSS + JS)
Web development typically involves three core
technologies:
·
HTML (HyperText Markup Language): Structures the content
on the page, like headings, paragraphs, images, and links.
·
CSS (Cascading Style Sheets): Styles the content,
controlling colors, fonts, layouts, and responsiveness.
·
JavaScript: Adds behavior and interactivity — for
example, form validation, animations, content updates without reloading, and
much more.
Together, these three create the complete user
experience on the web: HTML builds the skeleton, CSS dresses it up, and
JavaScript brings it to life.
Writing JavaScript: Inline, Internal,
External
You can add JavaScript to your webpages in
three ways:
1.
Inline JavaScript
JavaScript code written directly inside HTML elements, usually within event
attributes like onclick
.
<button
onclick=
"alert('Hello!')">Click Me
</button>
Note: Inline scripts are simple but generally discouraged
for larger projects due to maintainability and separation of concerns.
2.
Internal JavaScript
JavaScript code placed inside a <script>
tag within the HTML
document, usually inside the <head>
or at the end of the <body>
.
<script>
function
greet() {
alert(
'Welcome!');
}
</script>
<button
onclick=
"greet()">Click Me
</button>
3.
External JavaScript
JavaScript code written in separate .js
files and linked to your HTML using
the <script>
tag with a src
attribute.
<script
src=
"script.js">
</script>
This method is
preferred for larger projects because it keeps HTML and JavaScript separate,
improving organization and reusability.