Javascript operators

We study what an operator is and what it is for. We see Javascript operators, in various classifications, arithmetic, assignment, comparison, conditional, bitwise, and operator preference.

In this article we are going to start a series of texts focused on explaining the different operators available in this programming language.

When developing programs in any language, operators are used, which are used to make the calculations and operations necessary to carry out your objectives. Even the smallest program imaginable needs operators to do things, since a program that doesn’t perform operations would only limit itself to always doing the same thing.

It is the result of the operations that makes a program vary its behavior according to the data it has to work with and offer us results that are relevant to the user who uses it. There are simpler or more complex operations that can be performed with different types of operands, such as numbers or texts. We will see in this chapter and the following ones, in detail, all these operators available in Javascript.

Operator Usage Examples

Before listing the different types of operators, we are going to see a couple of examples of these to help us get a more accurate idea of ​​what they are. In the first example we are going to perform a sum using the sum operator.

3+5

This is a very basic expression that doesn’t make much sense on its own. It does the addition between the two operands number 3 and 5, but it doesn’t do much good because nothing is done with the result. Usually more than one operator is combined to create more useful expressions. The following expression is a combination between two operators, one performs a mathematical operation and the other is used to store the result.

myVariable = 23 * 5

In the example above, the * operator is used to perform a multiplication and the = operator is used to assign the result to a variable, so that we save the value for later use.

Operators can be classified according to the type of actions they perform. Next we will see each of these groups of operators and describe the function of each one.

arithmetic operators

They are used to carry out simple mathematical operations such as addition, subtraction or multiplication. In javascript they are:

+ Addition of two values

– Subtraction of two values, it can also be used to change the sign of a number if we use it with a single operand -23

* Multiplication of two values

/ Division of two values

% The remainder of the division of two numbers (3%2 would return 1, the remainder of dividing 3 by 2)

++ Increment by one unit, used with a single operand

— Decrement by one unit, used with a single operand

See also  HTML5 WebStorage API

examples

price = 128 //I enter a 128 in the variable price units = 10 //another assignment, then we will see assignment operators invoice = price * units //multiply price by units, I get the value invoice remainder = invoice % 3 //I obtain the remainder of dividing the invoice variable by 3 price++ //increases the price by one unit (now it is worth 129)

Assignment operators

They are used to assign values ​​to variables, we have already used the assignment operator = in previous examples, but there are other operators of this type, which come from the C language and that many of the readers will already know.

= Assignment. Assigns the right part of the equal to the left part. The final values ​​are placed on the right and a variable where we want to save the data is generally placed on the left.

+= Assignment with addition. Performs the sum of the part on the right with the part on the left and saves the result in the part on the left.

-= Assignment with subtraction

*= Multiplication Assignment

/= division assignment

%= Get the rest and assign

examples

savings = 7000 //assign 7000 to the savings variable savings += 3500 //increase the savings variable by 3500, now it is worth 10500 savings /= 2 //divide my savings by 2, now 5250 remain

In the following article we will continue learning about other Javascript operators: .

string operators

Character strings, or text variables, also have their own operators for performing typical actions on strings. Although javascript only has one operator for strings, other actions can be performed with a series of predefined functions in the language that we will see later.

+ Concatenates two strings, pasting the second string after the first.

Example

string1 = “hello” string2 = “world” concatenatedString = string1 + string2 //concatenated string equals “helloworld”

An important detail that can be seen in this case is that the operator + is used for two different uses: if its operands are numbers, it adds them, but if they are strings, it concatenates them. This happens in general with all the operators that are repeated in the language, javascript is smart enough to understand what type of operation to perform by checking the types that are involved in it.

One case that would be confusing is the use of the + operator when performing the operation with intermixed text and numeric operators. In this case, javascript assumes that you want to perform a concatenation and treats the two operands as if they were character strings, even if the text string we have is a number. We will see this more easily with the following example.

myNumber = 23 myString1 = “pepe” myString2 = “456” result1 = myNumber + myString1 //result1 is equal to “23pepe” result2 = myNumber + myString2 //result2 is equal to “23456” myString2 += myNumber //myString2 is now equal to “45623”

See also  Number format in PHP

As we have seen, also in the case of the += operator, if we are dealing with strings of text and numbers mixed together, it will treat the two operators as if they were strings.

Note: As you might have guessed, we are missing many typical operations to perform with strings, for which there are no operators. It is because those functionalities are obtained through the , which we will see later.

Logical operators

These operators are used to perform logical operations, which are those that result in true or false, and are used to make decisions in our scripts. Instead of working with numbers, Boolean operands are used to perform these types of operations, which we met previously, which are true (true) and false (false). Logical operators relate Boolean operands to result in another Boolean operand, as we can see in the following example.

If I’m hungry and I have food then I start to eat

Our Javascript program would in this example use a Boolean operand to make a decision. First it will check if I am hungry, if it is true (true) it will check if I have food. If both are true, it can be eaten. In case I don’t have food or I’m not hungry I wouldn’t eat, just like if I’m not hungry or food. The operand in question is the operand Y, which will be true if both operands are true.

Note: Not to be misled, it is safe to say that logical operators can be used in combination with data types other than booleans, but in this case we must use them in expressions that convert them to booleans. In the next group of operators that we are going to discuss in this article, we will talk about conditional operators, which can be used together with logical operators to carry out as complex sentences as we need. For example: if (x==2 && y!=3){ //the variable x is equal to 2 and the variable y is different from three } In the previous conditional expression we are evaluating two tests that are related to a logical operator. On the one hand, x==2 will return true if the variable x is equal to 2, and on the other, y!=3 will return true when the variable y has a value other than 3. Both checks return a boolean each, which is then the logical operator && is applied to it to check if both checks were fulfilled at the same time.

Needless to say, to see examples of conditional operators, we need to learn control structures like if, which we haven’t gotten to yet.

! NOT operator or negation. If it was true, it becomes false and vice versa.

&& Operator AND, if they are both true it is true.

|| Operator OR, is true if at least one of them is true.

See also  Is the tbody tag necessary to mount a table in HTML?

Example

myBoleano = true myBoleano = !myBoleano //myBoleano is now false I’mHungry = true I’mFood = true asFood = I’mHungry && I’mFood

conditional operators

They are used to make conditional expressions as complex as we want. These expressions are used to make decisions based on the comparison of several elements, for example if one number is greater than another or if they are equal.

Note: Of course, conditional operators are also used to make expressions that compare other types of data. Nothing prevents you from comparing two strings, to see if they are the same or different, for example. We could even compare booleans.

Conditional operators are used in conditional expressions for decision making. Since these conditional expressions will be studied later, it is better to describe the conditional operators later. Anyway here we can see the table of conditional operators.

== Check if two values ​​are equal

!= Checks if two values ​​are distinct

> Greater than, returns true if the first operand is greater than the second

< Less than, true when the element on the left is less than the element on the right
>= greater equal

<= Less than equal

We’ll see examples of conditional operators when we discuss control structures, such as the if conditional.

In addition, in this text we will see a matter of considerable importance in programming in general, which is the precedence of operators, which we must take into account whenever we use different operators in the same expression, so that they are related to each other and resolved. just the way we had planned.

Bitwise operators

These are very rare and you may never get to use them. Its use is made to carry out operations with zeros and ones. All that a computer handles are zeros and ones, although we use numbers and letters for our variables, these values ​​are actually written internally in the form of zeros and ones. In some cases we may need to perform operations treating the variables as zeros and ones and for this we will use these operands. In this manual it is a bit too big for us to make a discussion about this type of operators, but here you will be able to see these operators in case you need them one day.

&Y of bits

^ Bit Xor

| or bit

<< >> >>> >>>= >>= <<= Various kinds of changes

operator precedence

The evaluation of…

Loading Facebook Comments ...
Loading Disqus Comments ...