Home > Web Front-end > JS Tutorial > body text

Use JavaScript functions to perform mathematical calculations and logical judgments

王林
Release: 2023-11-04 13:54:39
Original
1103 people have browsed it

Use JavaScript functions to perform mathematical calculations and logical judgments

Use JavaScript functions for mathematical calculations and logical judgments

Among modern programming languages, JavaScript is a powerful language that can not only perform basic mathematical calculations, Logical judgments can also be made. This article will introduce how to use JavaScript functions to perform mathematical calculations and logical judgments, and provide specific code examples.

1. Mathematical calculation

  1. Addition calculation

Addition calculation is one of the most common operations in mathematics. In JavaScript, you can use functions to implement addition calculations.

Code example:

function add(a, b) {
  return a + b;
}

console.log(add(5, 3));  // 输出结果:8
Copy after login
  1. Subtraction calculation

Subtraction calculation is also one of the operations frequently used in mathematics. You can also use functions to implement subtraction calculation. .

Code example:

function subtract(a, b) {
  return a - b;
}

console.log(subtract(5, 3));  // 输出结果:2
Copy after login
  1. Multiplication calculation

Multiplication calculation can be implemented using functions.

Code example:

function multiply(a, b) {
  return a * b;
}

console.log(multiply(5, 3));  // 输出结果:15
Copy after login
  1. Division calculation

Division calculation can also be implemented using functions.

Code example:

function divide(a, b) {
  return a / b;
}

console.log(divide(6, 3));  // 输出结果:2
Copy after login

2. Logical judgment

Logical judgment is often used in programming to determine the execution path of the program based on conditions. JavaScript provides a variety of logical operators to implement logical judgments.

  1. Conditional judgment

Conditional judgment determines the execution path of the program based on whether the condition is true or false. The most commonly used conditional judgment in JavaScript is the if statement.

Code example:

function checkNumber(number) {
  if (number > 0) {
    console.log("The number is positive.");
  } else if (number < 0) {
    console.log("The number is negative.");
  } else {
    console.log("The number is zero.");
  }
}

checkNumber(5);  // 输出结果:The number is positive.
checkNumber(-3); // 输出结果:The number is negative.
checkNumber(0);  // 输出结果:The number is zero.
Copy after login
  1. Logical operations

Logical operations are used to combine multiple conditions. Commonly used logical operators are && (logical AND), || (logical OR), ! (logical NOT).

Code examples:

function checkAge(age) {
  if (age >= 18 && age <= 60) {
    console.log("You are an adult.");
  } else if (age < 18 || age > 60) {
    console.log("You are not an adult.");
  }
}

checkAge(25);  // 输出结果:You are an adult.
checkAge(15);  // 输出结果:You are not an adult.
checkAge(65);  // 输出结果:You are not an adult.
Copy after login

The above are code examples that use JavaScript functions to perform mathematical calculations and logical judgments. Through functions, we can encapsulate mathematical calculations and logical judgments to facilitate reuse and maintenance. At the same time, JavaScript provides a wealth of mathematical functions and logical operators, allowing programmers to easily perform mathematical calculations and logical judgment operations.

The above is the detailed content of Use JavaScript functions to perform mathematical calculations and logical judgments. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!