Join Regular Classroom : Visit ClassroomTech

JavaScript – codewindow.in

Related Topics

HTML

Introduction
Html page 1
Html page 2
Html page3
Html page4

HTML Elements and structure
Html page 5
Html page 6
Html page 7

HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10

HTML Lists and Tables
Html page 11
Html page 12
Html page 13

HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16

HTML Images and Media
Html page 17
Html page 18

HTML Links and Anchors
Html page 19
Html page 20
Html page 21

HTML Styles and Formatting
Html page 22

HTML Semantic Elements
Html page 23
Html page 24

HTML Attributes
Html page 25
Html page 26

HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30

HTML Document and Browser Support
Html page 31
Html page 32

HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36

HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39

HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42

JAVASCRIPT

// Function
function calculateArea(radius) {
  return Math.PI * radius * radius;
}

console.log(calculateArea(5)); // Output: 78.53981633974483

// Method
const circle = {
  radius: 5,
  calculateArea: function() {
    return Math.PI * this.radius * this.radius;
  }
};

console.log(circle.calculateArea()); // Output: 78.53981633974483
In the example above, calculateArea is a standalone function that calculates the area of a circle. It can be invoked directly using its name. On the other hand, the calculateArea method is associated with the circle object, accessed using circle.calculateArea(). It uses the object’s properties (this.radius) to perform the calculation.
In summary, while both functions and methods are blocks of code that perform a specific task, methods are specifically associated with an object and have access to the object’s properties and other methods using the this keyword.
function greet(name = 'friend') {
  console.log(`Hello, ${name}!`);
}

greet(); // Output: Hello, friend!
greet('John'); // Output: Hello, John!
In the example above, the greet function has a single parameter name with a default value of 'friend'. If no argument is passed or if the argument is undefined, the default value 'friend' is used.
Example 2: Using default parameters with multiple parameters
function multiply(a, b = 1) {
  return a * b;
}

console.log(multiply(5)); // Output: 5
console.log(multiply(3, 4)); // Output: 12
In this example, the multiply function has two parameters: a and b. The b parameter has a default value of 1. If the b argument is not provided or is undefined, the default value of 1 is used.
Example 3: Using expressions as default parameter values
function repeatString(string, count = string.length) {
  return string.repeat(count);
}

console.log(repeatString('Hello')); // Output: HelloHelloHelloHelloHello
console.log(repeatString('Hi', 3)); // Output: HiHiHi
In this example, the repeatString function has two parameters: string and count. The default value of count is set to the length of the string parameter. If the count argument is not provided, the default value will be the length of the string.
Default parameters provide a convenient way to define fallback values in functions and make the function calls more flexible by allowing some arguments to be optional.
function higherOrderFunction(callback) {
  console.log('Performing some operations before invoking the callback...');
  callback();
  console.log('Operations after invoking the callback.');
}

function callbackFunction() {
  console.log('This is the callback function.');
}

higherOrderFunction(callbackFunction);
In the code above, higherOrderFunction is a higher-order function that takes callbackFunction as an argument. When higherOrderFunction is called, it executes some operations before invoking the callback and then performs operations after invoking the callback. The callbackFunction is passed as an argument and is invoked within the higher-order function.
Higher-order functions are widely used in JavaScript, especially in scenarios such as event handling, asynchronous operations, functional programming patterns like map, filter, and reduce, and in creating reusable utility functions.

      

Popular Category

Topics for You

HTML

Introduction
Html page 1
Html page 2
Html page3
Html page4

HTML Elements and structure
Html page 5
Html page 6
Html page 7

HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10

HTML Lists and Tables
Html page 11
Html page 12
Html page 13

HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16

HTML Images and Media
Html page 17
Html page 18

HTML Links and Anchors
Html page 19
Html page 20
Html page 21

HTML Styles and Formatting
Html page 22

HTML Semantic Elements
Html page 23
Html page 24

HTML Attributes
Html page 25
Html page 26

HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30

HTML Document and Browser Support
Html page 31
Html page 32

HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36

HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39

HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42

Go through our study material. Your Job is awaiting.

Recent Posts
Categories