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
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
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
Multiplication calculation can be implemented using functions.
Code example:
function multiply(a, b) { return a * b; } console.log(multiply(5, 3)); // 输出结果:15
Division calculation can also be implemented using functions.
Code example:
function divide(a, b) { return a / b; } console.log(divide(6, 3)); // 输出结果:2
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.
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.
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.
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!