Table of Contents
Welcome to JavaScript!
JavaScript is a powerful programming language that brings websites to life. It's the language of the web, running in browsers and servers worldwide.
What is JavaScript?
JavaScript is a high-level, interpreted programming language that allows you to:
- Make websites interactive
- Create dynamic content
- Handle user interactions
- Build web applications
Your First JavaScript Code
Let's start with the classic "Hello, World!" example:
// This is a comment
console.log("Hello, World!");
// You can also use alert (though it's less common now)
alert("Welcome to JavaScript!");
Where to Write JavaScript
JavaScript can be written in three places:
1. Inline JavaScript
<button onclick="alert('Button clicked!')">Click Me</button>
2. Internal JavaScript
<script>
console.log("This runs when the page loads");
</script>
3. External JavaScript (Recommended)
<script src="script.js"></script>
JavaScript Syntax Rules
- Statements end with semicolons (;)
- JavaScript is case-sensitive
- Use camelCase for variable names
- Comments start with // for single line or /* */ for multiple lines
Practice Exercise
Create an HTML file and add JavaScript to display your name in the console:
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript</title>
</head>
<body>
<h1>JavaScript Basics</h1>
<script>
console.log("My name is [Your Name]");
console.log("I'm learning JavaScript!");
</script>
</body>
</html>