0% found this document useful (0 votes)
0 views

Introductory JavaScript

JavaScript is a high-level, interpreted programming language primarily used for web development, created by Brendan Eich in 1995. It supports dynamic typing, functional and object-oriented programming, and can manipulate HTML and CSS for interactive web pages. Key features include variables, data types, operators, conditional statements, loops, functions, arrays, and event handling.

Uploaded by

kevinowenhunt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Introductory JavaScript

JavaScript is a high-level, interpreted programming language primarily used for web development, created by Brendan Eich in 1995. It supports dynamic typing, functional and object-oriented programming, and can manipulate HTML and CSS for interactive web pages. Key features include variables, data types, operators, conditional statements, loops, functions, arrays, and event handling.

Uploaded by

kevinowenhunt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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 (Node.js).

• 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>

console.log("Hello, JavaScript!");

</script>

Or in an external JavaScript file (script.js):

console.log("Hello, JavaScript!");
And linked in HTML like this:

<script src="script.js"></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

console.log("Hello, World!"); // Print to console

alert("Welcome!"); // Show alert box

// Input

let name = prompt("Enter your name: ");

console.log("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) {

console.log("You are an adult.");

} else {

console.log("You are a minor.");

}
Loops in JavaScript

For Loop

for (let i = 0; i < 5; i++) {

console.log("Iteration: " + i);

}
While Loop

let count = 0;

while (count < 5) {

console.log(count);

count++;

Functions

// Function without parameters

function greet() {

console.log("Hello!");

greet(); // Call the function

// Function with parameters & return value

function add(a, b) {

return a + b;

console.log(add(5, 3)); // Output: 8


Arrays in JavaScript

let colors = ["Red", "Green", "Blue"];

console.log(colors[0]); // Output: Red

console.log(colors.length); // Output: 3

colors.push("Yellow"); // Adds a new element

console.log(colors);

Objects in JavaScript

let person = {

name: "Alice",

age: 25,

greet: function() {

console.log("Hello, " + this.name);

};

console.log(person.name); // Output: Alice

person.greet(); // 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: "));

console.log("Sum:", num1 + num2);

You might also like