Introductory JavaScript
What is JavaScript?
• JavaScript (JS) is a high-level, interpreted programming language.
• Created by Brendan Eich in 1995.
• It is mainly used for web development (making websites interactive).
• JavaScript runs in the browser and is used for both front-end and back-end
development ([Link]).
• Works with HTML and CSS to create dynamic web pages.
Basic Features of JavaScript
• Lightweight and fast (runs directly in browsers).
• Dynamically typed (no need to declare variable types).
• Supports both functional and object-oriented programming.
• Event-driven (reacts to user actions like clicks).
• Can manipulate HTML & CSS (DOM Manipulation).
JavaScript Syntax Basics
Writing JavaScript
JavaScript can be written inside an HTML file using <script> tags:
<script>
[Link]("Hello, JavaScript!");
</script>
Or in an external JavaScript file ([Link]):
[Link]("Hello, JavaScript!");
And linked in HTML like this:
<script src="[Link]"></script>
Comments in JavaScript
// This is a single-line comment
/*
This is a
multi-line comment
*/
Variables & Data Types
Declaring Variables
var name = "Alice"; // Old way (avoid using var)
let age = 25; // Modern way (changeable variable)
const PI = 3.14159; // Constant (cannot change)
Data Types
Type Example
Number 5, 3.14, -10
String "Hello", 'JavaScript'
Boolean true, false
Array [1, 2, 3]
Object {name: "Alice", age: 25}
null null
undefined undefined
Input and Output
// Output
[Link]("Hello, World!"); // Print to console
alert("Welcome!"); // Show alert box
// Input
let name = prompt("Enter your name: ");
[Link]("Hello, " + name);
Operators in JavaScript
Operator Example Meaning
+ a+b Addition
- a-b Subtraction
* a*b Multiplication
/ a/b Division
% a%b Modulus
== a == b Equal to (checks only value)
=== a === b Strict equal (checks value & type)
!= a != b Not equal
Conditional Statements
let age = 20;
if (age >= 18) {
[Link]("You are an adult.");
} else {
[Link]("You are a minor.");
}
Loops in JavaScript
For Loop
for (let i = 0; i < 5; i++) {
[Link]("Iteration: " + i);
}
While Loop
let count = 0;
while (count < 5) {
[Link](count);
count++;
Functions
// Function without parameters
function greet() {
[Link]("Hello!");
greet(); // Call the function
// Function with parameters & return value
function add(a, b) {
return a + b;
[Link](add(5, 3)); // Output: 8
Arrays in JavaScript
let colors = ["Red", "Green", "Blue"];
[Link](colors[0]); // Output: Red
[Link]([Link]); // Output: 3
[Link]("Yellow"); // Adds a new element
[Link](colors);
Objects in JavaScript
let person = {
name: "Alice",
age: 25,
greet: function() {
[Link]("Hello, " + [Link]);
};
[Link]([Link]); // Output: Alice
[Link](); // Output: Hello, Alice
Event Handling (DOM Manipulation)
<button onclick="sayHello()">Click Me</button>
<script>
function sayHello() {
alert("Hello, JavaScript!");
</script>
Example Program: Simple Calculator
let num1 = parseFloat(prompt("Enter first number: "));
let num2 = parseFloat(prompt("Enter second number: "));
[Link]("Sum:", num1 + num2);