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 functionName(parameters) {
  // body of the function
}
Function expression is an expression that evaluates to a function. It is a function that is defined inline, and it is not hoisted.
The syntax for a function expression is:
const functionName = function(parameters) {
  // body of the function
};
The main difference between function declaration and function expression is that function declarations are hoisted, while function expressions are not. This means that function declarations are available to the rest of the code before they are defined, while function expressions are not.
Another difference between function declaration and function expression is that function declarations can be used to define named functions, while function expressions can only be used to define anonymous functions.
Here is an example of a function declaration:
function factorial(n) {
  if (n === 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}
This function declaration defines a function called factorial. The function takes a number as an argument and returns the factorial of the number.
Here is an example of a function expression:
const factorial = function(n) {
  if (n === 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
};
This function expression defines an anonymous function called factorial. The function takes a number as an argument and returns the factorial of the number.
The main difference between these two examples is that the first example defines a named function, while the second example defines an anonymous function.
Functions are a powerful tool in JavaScript. They can be used to encapsulate code, reuse code, and perform complex tasks. Function declarations and function expressions are two ways to define functions in JavaScript. They have different properties, and they are used in different contexts.
// Define a function
function factorial(n) {
  if (n === 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

// Call the function
const result = factorial(5);
console.log(result); // 120
The first line defines a function called factorial. The function takes a number as an argument and returns the factorial of the number. The function body is indented, and it contains two statements. The first statement checks if the argument is equal to 0. If it is, the function returns 1. Otherwise, the function returns the product of the argument and the factorial of the argument minus 1.
The second line calls the factorial function. The function is called with the argument 5. The function returns the value 120, and the value is logged to the console.
To define a function in JavaScript, you use the function keyword. The function keyword is followed by the name of the function, the parentheses, and the body of the function. The body of the function is a block of code that is executed when the function is called.
To call a function in JavaScript, you use the name of the function followed by the parentheses and the arguments. The arguments are the values that are passed to the function when it is called.
// Define a function that takes two numbers as arguments
function addNumbers(number1, number2) {
  return number1 + number2;
}

// Call the function with the arguments 10 and 20
const result = addNumbers(10, 20);
console.log(result); // 30

// Define a function that takes an array as an argument
function printArray(array) {
  for (const element of array) {
    console.log(element);
  }
}

// Call the function with the array [1, 2, 3, 4, 5]
printArray([1, 2, 3, 4, 5]);
In the first example, the function addNumbers takes two numbers as arguments and returns the sum of the numbers. The function is called with the arguments 10 and 20. The function returns the value 30, and the value is logged to the console.
In the second example, the function printArray takes an array as an argument and prints the elements of the array to the console. The function is called with the array [1, 2, 3, 4, 5]. The function prints the elements of the array to the console.
In both of these examples, the parameters are passed to the function when the function is called. The parameters are then used by the function to perform its task.
Here are some rules for passing parameters to a function in JavaScript:
  • The number of parameters must match the number of arguments.
  • The types of the parameters must match the types of the arguments.
  • The order of the parameters must match the order of the arguments.
function changeNumber(number) {
  number = number + 1;
}

const originalNumber = 10;
changeNumber(originalNumber);
console.log(originalNumber); // 10
In this example, the function changeNumber takes a number as a parameter. The parameter number is passed by value to the function. When the function is called, a copy of the value of originalNumber is made and passed to the function. The function then changes the value of the copy to 11. However, the original value of originalNumber is not changed.
Here is an example of pass-by-reference in JavaScript:
function changeArray(array) {
  array.push(10);
}

const originalArray = [1, 2, 3];
changeArray(originalArray);
console.log(originalArray); // [1, 2, 3, 10]
In this example, the function changeArray takes an array as a parameter. The parameter array is passed by reference to the function. When the function is called, the reference to the array originalArray is passed to the function. The function then adds the element 10 to the array. The change is made to the original array, and the value of originalArray is updated to [1, 2, 3, 10].
The main difference between pass-by-value and pass-by-reference is that pass-by-value passes a copy of the value, while pass-by-reference passes a reference to the value. This means that changes made to the value in the function affect the original value in pass-by-value, but they do not affect the original value in pass-by-reference.
Pass-by-value is the default way to pass parameters in JavaScript. Pass-by-reference is used less often, but it is useful when you need to make changes to the original value in the function.

      

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