Core points
if
, else
and switch
control the execution process according to different conditions, so that the code can make decisions. &&
, ||
, !
) are used to combine multiple conditions or invert Boolean values and play a key role in complex decisions. 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:
if
statement can be used to execute code blocks only when a specific condition is met. 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
if
/else
if
else
else if
switch
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";
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
Subtraction
The subtraction operator is represented by a minus sign (-), which subtracts two values and returns the difference.
const continent = "Europe";
Multiple
The multiplication operator is represented by an asterisk (*), which multiplies two values and returns the product.
const x = 2 + 2; // x 返回 4
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
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
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
This happens after the assignment. It can also be written as x;
, which occurs before assignment. Comparison:
const x = 10 % 3; // 返回 1
and:
let x = 10; x++; // x 返回 11
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
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!