SimpleTuts.com

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:

2. Where JavaScript is Used

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>

4. Your First JavaScript Program


console.log("Welcome to JavaScript Learning");

5. Real Life Example: Greeting Application

This example greets the user when they enter their name.

index.html


<!DOCTYPE html>
<html>
<head>
<title>Greeting App</title>
</head>
<body>

<h2>Greeting Application</h2>

<input type="text" id="name" placeholder="Enter your name">
<button onclick="greetUser()">Greet</button>

<p id="message"></p>

<script src="app.js"></script>

</body>
</html>
Run Code

app.js


function greetUser() {
    let name = document.getElementById("name").value;
    document.getElementById("message").innerText = "Hello " + name + "! Welcome to JavaScript.";
}
Run Code

6. How This Example Works

  1. User enters their name in the input box.
  2. When the button is clicked, the function greetUser() runs.
  3. JavaScript reads the value from the input field.
  4. The message is displayed on the page.

7. Practice Exercise

Create a small application that:

8. What Students Learned

Next Chapter

Chapter 2: Variables, Data Types, and Operators