JavaScript โ Lesson 1
Introduction to JavaScript
What JavaScript is and how to use it.
What is JavaScript?
JavaScript (JS) is a programming language that makes web pages interactive. It runs in the browser and can respond to user actions, manipulate HTML/CSS, and communicate with servers.
Where JavaScript Runs
- Client-side: In the browser (Chrome, Firefox, Safari, Edge)
- Server-side: On the server with Node.js (advanced)
Adding JavaScript to a Page
1. Inline Script
<script>
console.log("Hello, World!");
</script>
2. External File (Recommended)
<!-- In HTML, usually before </body> -->
<script src="script.js"></script>
Your First JavaScript
<!DOCTYPE html>
<html>
<body>
<h1 id="demo">Hello, World!</h1>
<button onclick="sayHello()">Click Me</button>
<script>
function sayHello() {
document.getElementById('demo').innerHTML = 'Hello from JavaScript!';
}
</script>
</body>
</html>
Statements
JavaScript instructions are called statements. Each statement ends with a semicolon ; (optional but recommended).
// Three statements
let x = 5;
let y = 10;
let sum = x + y;
Comments
// Single-line comment
/*
Multi-line
comment
*/
Try It
Open the Deoit Editor, create a JS file, and type: console.log("Hello!"); then click Run. Check the Console panel to see the output!
Tip: Use
console.log() often - it's the most important debugging tool in JavaScript!
Practice what you learned in the Deoit Editor โ a free, browser-based code editor.
Open Editor โ