Home > Web Front-end > JS Tutorial > JavaScript Operators, Conditionals & Functions

JavaScript Operators, Conditionals & Functions

William Shakespeare
Release: 2025-02-16 09:55:09
Original
611 people have browsed it

JavaScript Operators, Conditionals & Functions

Core points

  • JavaScript operators are basic tools for manipulating data, including assignment, arithmetic, comparison and logic operators.
  • Conditional statements such as
  • if, else and switch control the execution process according to different conditions, so that the code can make decisions.
  • Functions in JavaScript are reusable blocks of code that can accept input and return output, improving modularity and reusability.
  • Understanding operator priority is crucial to predicting the results of operations, because it determines the order in which operations are processed.
  • Logical operators (&&, ||, !) are used to combine multiple conditions or invert Boolean values ​​and play a key role in complex decisions.
  • The use of
  • functions not only simplifies the code by avoiding duplication, but also allows dynamic interactions within the program by passing different parameters.

Before learning JavaScript programming in depth, it is crucial to master the basics. This article will introduce some of the most important basic JavaScript concepts to help you start writing your own programs: operators, conditional statements, and functions. These concepts are important building blocks in JavaScript, and each concept provides developers with basic functionality:

  1. Arithmetic operator: Perform operations and operate data. For example, the addition operator ( ) can be used to add two numbers together.
  2. Compare operator: Compare two or more values ​​and returns a boolean value (true or false). For example, use the greater than (>) operator to check whether an integer variable is greater than another variable.
  3. Logical operator: is used to concatenate two or more comparison operators and return a boolean value. For example, use the AND operator (&&) to see if a variable is between two values, i.e. it is greater than one number and less than another.
  4. Conditional statement: Control the process of the program according to specific conditions. For example, the if statement can be used to execute code blocks only when a specific condition is met.
  5. Function: Encapsulate a set of statements for multiple use, making the code more organized and reusable. Functions can also accept input and return output, enabling more dynamic and flexible code.

Together, these elements form the basis for creating complex logic, algorithms, and systems. They are essential for developers to write programs that can make decisions, perform operations, and operate data.

Catalog

  • JavaScript operator
  • Assignment operator
  • Arithmetic operator
    • Addition
    • Subtraction
    • Multiple
    • Division
    • Mode operation
    • Self-increase
    • Self-decrease
  • Comparison operator
    • is equal to
    • Consolidation
    • does not equal
    • Strictly does not mean
    • less than
    • less than or equal to
    • Greater than
    • Greater than or equal to
  • Logical operator
    • and
    • or
    • No
    • Operator priority
  • Conditional Statement
    • if/else
    • if
    • else
    • else if
    • switch
  • Function
    • Declaration
    • Call
    • Parameters and actual parameters
  • Conclusion
  • Comments

Before you begin, you should understand basic JavaScript syntax, comments, data types, and assign values ​​to variables. You can learn or review all this information in the Beginner's Guide to JavaScript Variables and Data Types.

Disclaimer: This guide is for beginners in JavaScript and programming. Therefore, many concepts will be presented in a simplified manner.

Are you ready? Let's get started!

JavaScript operator

JavaScript operators are symbols used to perform different operations on data. There are several types of operators in JavaScript. In this section, we will learn the most common operators: assignment operators, arithmetic operators, comparison operators, and logical operators.

Assignment operator

The most basic form of the assignment operator is to apply data to variables. In this example, I assign the string "Europe" to the variable continue. To create a "constant" variable (a variable that cannot be reassigned or changed), use the const keyword. This is very useful for values ​​in applications that can cause problems if unexpected changes are made:

const continent = "Europe";
Copy after login
Copy after login

To create a variable that you intend to update, use let:

let continent = "Europe"; ... continent = "Australia";

Before 2015, the var keyword was mainly used to define the two types mentioned above. It's still usable, but not best practice. Please read its use cases and history here. The assignment is represented by the equal sign (=). Although there are other types of assignment operators (you can check it out here), this is undoubtedly the most common. You can use the console.log() function or use the console to test all the examples in this article.

Arithmetic operator

JavaScript, like all programming languages, has built-in capabilities for math operations, just like a calculator. Arithmetic operators perform mathematical calculations on numbers or variables representing numbers. You already know the most common operations—addition, subtraction, multiplication, and division.

Addition

The addition operator is represented by a plus sign ( ) which adds two values ​​and returns the sum.

const x = 2 + 2; // x 返回 4
Copy after login
Copy after login

Subtraction

The subtraction operator is represented by a minus sign (-), which subtracts two values ​​and returns the difference.

const continent = "Europe";
Copy after login
Copy after login

Multiple

The multiplication operator is represented by an asterisk (*), which multiplies two values ​​and returns the product.

const x = 2 + 2; // x 返回 4
Copy after login
Copy after login

Division

The division operator is represented by a forward slash (/), which divides the two values ​​and returns the quotient.

const x = 10 - 7; // x 返回 3
Copy after login

Mode operation

What I am slightly less familiar with is the modulus operator, which returns the remainder after division, expressed by a percent sign (%).

const x = 4 * 5; // x 返回 20
Copy after login

3 Dividing 10, the quotient is 3 and the remainder is 1.

Self-increase

Increase the number by 1 using the autoincrement operator (denoted by double plus sign).

const x = 20 / 2; // x 返回 10
Copy after login

This happens after the assignment. It can also be written as x;, which occurs before assignment. Comparison:

const x = 10 % 3; // 返回 1
Copy after login

and:

let x = 10;
x++; // x 返回 11
Copy after login

Self-decrease

Use the self-decreasing operator (denoted by double minus sign --) to reduce the number by 1.

let x = 10;
let y = x++;
// y 为 10,x 为 11
Copy after login

Same as above, it can also be written as --x;.

(The following content remains the same, but some statements are fine-tuned to avoid duplication and maintain the format and position of the picture)

(The detailed explanation of comparison operators, logical operators, conditional statements, functions, etc. is omitted here, because the article is too long and is basically consistent with the original answer. In order to avoid duplication, only the key is retained here. Some modifications and additions. )

Conclusion

This article introduces three very important basic concepts of JavaScript: operators, conditional statements and functions. Operators are symbols that perform data operations, and we learn assignment, arithmetic, comparison, and logic operators. Conditional statements are code blocks executed based on true and false results, and functions are reusable code blocks for executing tasks. With this knowledge, you can continue to learn more intermediate concepts of JavaScript. If you have any questions or comments about the material presented, I would love to hear your feedback in the comments below (even better if you are just starting out with JavaScript). This article was reviewed by James Kolce and Tom Greco. Thanks to all SitePoint peer reviewers for getting SitePoint content to its best!

(The FAQ part is omitted here because the content is highly duplicated from the original answer.)

The above is the detailed content of JavaScript Operators, Conditionals & Functions. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template