Javascript Study Material
Javascript 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:
// Code to execute
return result;
Example:
function add(a, b) {
return a + b;
console.log(result); // Output: 8
Example:
function displayResult(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:
};
Use in Arrays:
Example:
numbers.forEach(function(num) {
console.log(num * 2);
});
// Output: 2, 4, 6, 8
3. Objects in JavaScript
Definition:
Example:
let person = {
name: "Alice",
age: 25,
greet: function() {
};
Example:
stack.push(1); // [1]
stack.push(2); // [1, 2]
queue.push(1); // [1]
queue.push(2); // [1, 2]
5. Regular Expressions
Definition:
Regular Expressions (RegEx) are patterns used to match strings.
Basic Syntax:
Character Classes:
Example:
Example:
Definition:
The DOM is an interface that represents the structure of an HTML document as a tree.
document.getElementById("id");
document.getElementsByClassName("class");
document.querySelector(".class");
Changing Content:
Example:
<!DOCTYPE html>
<html>
<body>
<script>
function changeText() {
</script>
</body>
</html>
7. Event Handling
Events:
Example:
<!DOCTYPE html>
<html>
<body>
<script>
function changeColor(element) {
element.style.backgroundColor = "red";
</script>
</body>
</html>
8. Form Handling
<!DOCTYPE html>
<html>
<body>
</form>
<script>
function validateForm() {
return false;
return true;
</script>
</body>
</html>
Methods:
Example:
alert("Proceeding!");
} else {
alert("Canceled.");
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:
</form>
<script>
function validate() {
alert("Invalid email!");
return false;
return true;
</script>