JavaScript Introduction
Getting Started with JavaScript
1. What is JavaScript?
JavaScript is a programming language used to create interactive and dynamic web pages. While HTML provides structure and CSS provides styling, JavaScript adds behavior to websites.
Example:
- Click a button and show a message
- Validate form input
- Update content without refreshing the page
2. Where JavaScript is Used
- Web development
- Mobile applications
- Server-side development (Node.js)
- Games
- Desktop applications
3. Adding JavaScript to HTML
Inline JavaScript
<button onclick="alert('Hello!')">Click Me</button>
Internal JavaScript
<script>
console.log("Hello JavaScript");
</script>
External JavaScript
<script src="script.js"></script>
script.js
console.log("Welcome to JavaScript Learning");
4. Real Life Example:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Using Functions</title>
</head>
<body>
<!-- inline js -->
<button onclick="alert('Welcome to JavaScript')">Click here</button>
<button onclick="callInternal()">Call Internal</button>
<button onclick="callExternal()">Call External</button>
<!-- Internal Js -->
<script>
function callInternal(){
alert('Alert from Internal JavaScript');
console.log("Console output from Internal JS");
}
</script>
<!-- External Js -->
<script src="myscript.js"></script>
</body>
</html>
Run Code
myscript.js
function callExternal(){
alert('Alert from External JavaScript');
console.log("Console output from External JS");
}
Run Code
5. What Students Learned
- What JavaScript is
- How to add JavaScript to HTML
- Basic interaction with HTML elements
- Writing a simple function
Next Chapter
Chapter 2: Variables, Data Types, and Operators