Introduction to JavaScript

Introduction to JavaScript

Introduction to JavaScript

JavaScript is one of the most popular programming languages in the world. It powers interactive websites, mobile apps, and even server-side applications. If you’ve ever clicked a button that updated content on a webpage without refreshing, chances are JavaScript was behind it.

In this blog post, we’ll cover:

  • What JavaScript is
  • Why it’s important
  • Basic JavaScript examples

What is JavaScript?

JavaScript (often shortened to JS) is a scripting language that allows developers to create dynamic and interactive web pages. Unlike HTML (which structures content) and CSS (which styles it), JavaScript makes websites responsive to user actions.

Why Learn JavaScript?

  • Used by 98% of all websites (according to W3Techs).
  • Works in browsers (front-end) and servers (back-end with Node.js).
  • Powers frameworks like React, Angular, and Vue.js.
  • Essential for web development jobs.

JavaScript Basics with Examples

1. JavaScript in HTML

<!DOCTYPE html>
<html>
<body>
  <h1>My First JavaScript</h1>
  <button onclick="alert('Hello World!')">Click Me</button>
</body>
</html>

This code shows an alert when the button is clicked.

2. Variables & Data Types

let name = "John";  // String
const age = 25;     // Number (constant, can't change)
let isStudent = true; // Boolean (true/false)
  • let allows reassignment.
  • const is for values that won’t change.

3. Functions

Functions are reusable blocks of code.

function greet(name) {
  return "Hello, " + name + "!";
}

console.log(greet("Alice")); // Output: "Hello, Alice!"

4. Events (Making Web Pages Interactive)

JavaScript responds to user actions like clicks, mouse movements, and key presses.

<button id="myButton">Click Me</button>
<script>
  document.getElementById("myButton").addEventListener("click", function() {
    alert("Button was clicked!");
  });
</script>

5. Conditional Statements (If-Else)

let score = 85;

if (score >= 90) {
  console.log("A Grade");
} else if (score >= 80) {
  console.log("B Grade"); // This runs
} else {
  console.log("C Grade");
}

Conclusion

JavaScript is essential for modern web development. It makes websites interactive, handles user inputs, and connects with servers.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *