0% found this document useful (0 votes)
11 views8 pages

Javascript Study Material

Uploaded by

Harsh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Javascript Study Material

Uploaded by

Harsh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JavaScript Exam Study Material

1. Functions in JavaScript

Definition:

A function is a block of code designed to perform a specific task. Functions can accept parameters and return values.

Syntax:

function functionName(parameter1, parameter2) {

// Code to execute

return result;

Passing and Returning Parameters:

Example:

function add(a, b) {

return a + b;

let result = add(5, 3); // Passing parameters 5 and 3

console.log(result); // Output: 8

Passing Function by Reference:

Example:

function displayResult(sumFunction, x, y) {

let result = sumFunction(x, y);

console.log("Result:", result);

function sum(a, b) {

return a + b;

}
displayResult(sum, 4, 6); // Output: Result: 10

2. Anonymous Functions

Definition:

Anonymous functions are functions without a name. They are often used as arguments to other functions or assigned to

variables.

Example:

let greet = function(name) {

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

};

greet("John"); // Output: Hello, John

Use in Arrays:

Example:

let numbers = [1, 2, 3, 4];

numbers.forEach(function(num) {

console.log(num * 2);

});

// Output: 2, 4, 6, 8

3. Objects in JavaScript

Definition:

Objects in JavaScript are collections of properties (key-value pairs).

Common Properties and Methods:

- hasOwnProperty(key): Checks if a property exists.

- Object.keys(object): Returns an array of keys.


- Object.values(object): Returns an array of values.

Example:

let person = {

name: "Alice",

age: 25,

greet: function() {

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

};

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

person.greet(); // Output: Hello, Alice

4. Arrays as Stacks and Queues

- Stack: Use push() and pop() (Last-In-First-Out).

- Queue: Use push() and shift() (First-In-First-Out).

Example:

let stack = [];

stack.push(1); // [1]

stack.push(2); // [1, 2]

stack.pop(); // Removes 2 -> [1]

let queue = [];

queue.push(1); // [1]

queue.push(2); // [1, 2]

queue.shift(); // Removes 1 -> [2]

5. Regular Expressions

Definition:
Regular Expressions (RegEx) are patterns used to match strings.

Basic Syntax:

let pattern = /hello/;

let result = pattern.test("hello world");

console.log(result); // Output: true

Character Classes:

- \d: Matches digits.

- \w: Matches word characters (a-z, A-Z, 0-9, _).

- \s: Matches whitespace.

Example:

let pattern = /\d+/; // Matches one or more digits

console.log(pattern.test("abc123")); // Output: true

Lookahead and Lookbehind:

Example:

let pattern = /\d(?=abc)/;

console.log("2abc".match(pattern)); // Output: ["2"]

6. DOM (Document Object Model)

Definition:

The DOM is an interface that represents the structure of an HTML document as a tree.

Accessing DOM Elements:

document.getElementById("id");

document.getElementsByClassName("class");

document.querySelector(".class");

Changing Content:

Example:
<!DOCTYPE html>

<html>

<body>

<h1 id="title">Hello World</h1>

<button onclick="changeText()">Click Me</button>

<script>

function changeText() {

document.getElementById("title").innerText = "Text Changed!";

</script>

</body>

</html>

7. Event Handling

Events:

- onclick: Triggered when an element is clicked.

- onmouseover: Triggered when the mouse hovers.

Example:

<!DOCTYPE html>

<html>

<body>

<div onmouseover="changeColor(this)" style="width:100px; height:100px; background-color:blue;"></div>

<script>

function changeColor(element) {

element.style.backgroundColor = "red";

</script>

</body>

</html>
8. Form Handling

Form Validation Example:

<!DOCTYPE html>

<html>

<body>

<form onsubmit="return validateForm()">

Name: <input type="text" id="name" />

<input type="submit" value="Submit" />

</form>

<script>

function validateForm() {

let name = document.getElementById("name").value;

if (name === "") {

alert("Name must not be empty!");

return false;

return true;

</script>

</body>

</html>

9. Window Methods and Features

Methods:

- alert(): Displays an alert box.

- confirm(): Displays a confirmation box.

Example:

if (confirm("Do you want to proceed?")) {

alert("Proceeding!");
} else {

alert("Canceled.");

10. Practice Programs

1. Calculator:

function calculate(op, a, b) {

switch (op) {

case "add":

return a + b;

case "subtract":

return a - b;

case "multiply":

return a * b;

case "divide":

return a / b;

default:

return "Invalid operation";

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

2. Form Validation Example:

<form onsubmit="return validate()">

Email: <input type="email" id="email" />

<input type="submit" />

</form>

<script>

function validate() {

let email = document.getElementById("email").value;


if (!email.includes("@")) {

alert("Invalid email!");

return false;

return true;

</script>

You might also like