Home > Web Front-end > JS Tutorial > javascript operators you must learn every day_javascript skills

javascript operators you must learn every day_javascript skills

WBOY
Release: 2016-05-16 15:15:21
Original
981 people have browsed it

Hello readers, I have already roughly understood the role of Javascript and some basic function declarations and variable declarations. Today we will continue to explain the previous content. Let’s take a look at the logic of javscript (forward sequence, branch , loop) and some simple operators

Let’s talk about some simple operation operators

  • + Plus sign: 1. Used to calculate the sum of two numbers, 2. Used to splice two strings
  • - Minus sign: 1. Used to calculate the difference between two numbers, 2. Used in front of a number to identify a negative number
  • * Multiplication sign: used to calculate the product of two numbers
  • / Division sign: used to calculate the quotient obtained after dividing the dividend by the divisor
  • % Remainder: Used to calculate the remainder after dividing the dividend by the divisor (the calculation here is an integer division, and the remainder after integer division will be obtained)
  • = Assignment: used to assign a value (can be an object, function, number, string, Boolean) to a variable

Here, we can clearly see that the operators display the results of the operations. I don’t need to go into details about addition, subtraction, multiplication and division. I believe everyone’s calculation skills should be better than mine, because I only graduated from middle school. That's it. What I want to talk about a little more is the remainder operation. In the above example, 5% 2 takes the remainder and gets 1. It is calculated like this, 5÷ 2 = 2... 1. You should have seen this in the second grade of elementary school. Calculate, dividend ÷ divisor = quotient...remainder, this remainder is the result of the remainder operation, and the equal symbol mentioned yesterday, which is the var temp = value used for assignment; we use the var keyword to To declare a variable, use = to assign a value to you. The type of the value can be an object, a function, a number, a string, or a Boolean. As I have told you before, what a function is is a function body declared with function. That is a function. , Needless to say numbers, strings are just a string of strings enclosed in quotation marks, and Boolean values ​​are a type with only two values, true and false, and there is no third one.

Let’s take a look at what the various values ​​look like

//这里我们给了一个空对象(null)
  var temp_1 = null;
  //这里我们给一个函数
  var temp_2 = function(){
   console.log("Write Something");
  };
  //这里我们给了一个字符串
  var temp_3 = "My name is MrDream";
  //这里我们给了一个boolean值
  var temp_4 = true;

Copy after login

These basic operations should be clear to everyone. I hope that readers will not be anxious when they see these basic things. We are still in the introductory introduction stage, so everyone must understand these basic things. In the article, I will talk about more things. Otherwise, if the foundation is not laid well, later, when I talk about something that you think is awesome, it will be meaningless if you can only copy the code and use it, and you cannot write it independently. Something you want.

Let’s further understand what the operator looks like after a small upgrade? (Note: At present, I only talk about some basic operators, and some special operators. I will explain them in some applications later, and then the understanding will be deeper)

  • += Addition: First add the two operands, and then assign the added value to the first operand
  • -= Subtraction: First subtract the two operands, and then assign the added value to the first operand
  • *= Multiplication and so on: first multiply the two operands, and then assign the added value to the first operand
  • /= Division, etc.: First divide the two operands, and then assign the added value to the first operand
  • %= Remainder: First perform the remainder operation on the two operands, and then assign the added value to the first operand

Here are some simple comparison operators

  • == Equal: Determine whether the objects on both sides of the operator are equal
  • === Absolute equality: Determine whether the objects on both sides of the operator are absolutely equal
  • !== Absolutely not equal: Determine whether the objects on both sides of the operator are absolutely unequal
  • != Not equal: Determine whether the objects on both sides of the operator are not equal
  • || Or (or): First determine whether the objects on the left and right sides of the operator are "non-empty objects or non-undefined objects". If both are, return the value on the right. If If there is a value that is not, return the value that is not that value
  • && And (and): First determine whether the objects on the left and right sides of the operator are "non-empty objects or non-undefined objects". If they are neither, return the value on the right. If there is a value If yes, return the value that is yes. If both are yes, return the one on the left.
  • ! Negation (not): After converting a value into a Boolean value, then negate it

(Note: The or and negation operators mentioned here are explained using everyone’s common understanding of logic. In fact, the internal operations involve the results of binary OR and NOT operations. We We’ll talk more about binary below) Let’s look at some examples

The following is a little bit about the operations of OR and NOT, which is a little more difficult to understand

Looking at the picture, at A, because there is no object defined on the left side of the operator, the value on the right is returned; at B, because the left side is an empty object, the value on the right is returned; at C, because there is a real value number 123 on the left , so 123 is returned; because the left side of D is the real value number -1, the value on the left is returned. One additional note: The number 0 is also a special value. If the left side is 0, then the value on the right will be returned.

To summarize the function of the OR operator: it is used to check whether the left and right sides of the operator are undefined values, null values, 0, false. If the left side is these values, the value on the right is returned. If the left side is not of these types value, return the value on the left.

Next and operation

Still talking from the picture, at A: the left and right are undefined objects and empty objects respectively, and the value on the left is returned; at B, the left is an empty object, so the left is returned; at C: the right is an undefined object, so the value on the right is returned ; D: Same as C; E: Both are non-empty objects, and the Boolean value on the left is true, so the right is returned; F: The same as E; G: Because the left is 0, it is the same as an undefined object , the empty object is the same level as false, so the left value is returned.

The last one is a non-operation

Continue to look at the picture and talk, at A: the operation is a value such as 0, false, null, undefined, etc., and when returned, the true value of the Boolean value is obtained; at B: the operation is to invert the real value, and the The main is the boolean value false. We can conclude that negation is a Boolean value, negation of an unreal value is correct, and negation of an actual value is an error.

Okay, that’s all for today. To summarize, today, I actually just talked about some very basic operators, but there are a lot of operators, so I just wrote this right to finish the expression. , and there are some special operators, which we will talk about later. As long as you make a little progress every day, it is not a dream to become a great master.

Related labels:
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