Skip to main content

Basic JavaScript Operators

Concepts about variables in JavaScript

Learn what basic JavaScript operators are and how to use them: arithmetic, comparison, logical, and assignment operators, with simple examples.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations. The most common are:

Addition+

Add two numbers.

let suma = 5 + 3; // Resultado: 8

Subtract-

Subtract one number from another.

let resta = 5 - 3; // Resultado: 2

Multiplication*

Multiply two numbers.

let multiplicacion = 5 * 3; // Resultado: 15

Division/

Divide one number by another.

let division = 6 / 3; // Resultado: 2

Module%

Returns the remainder of a division.

let modulo = 5 % 2; // Resultado: 1

Increase++

Increase the value of a variable by 1.

let x = 5;
x++; // Resultado: 6

Decrement--

Decrease the value of a variable by 1.

let y = 5;
y--; // Resultado: 4

Exponentiation**

Raise a number to the power of another.

let potencia = 2 ** 3; // Resultado: 8

Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (true either falseThe most common ones are:

Equality==

Checks if two values are equal, regardless of data type.

let esIgual = 5 == "5"; // Resultado: true

Inequality!=

Checks if two values are not equal, regardless of the data type.

let esDesigual = 5 != "5"; // Resultado: false

Strictly Equal===

Check if two values are equal and of the same data type.

let esEstrictamenteIgual = 5 === 5; // Resultado: true

Strictly Unequal!==

Check if two values are not equal or are not of the same data type.

let esEstrictamenteDesigual = 5 !== "5"; // Resultado: true

Greater than>

Check if one value is greater than another.

let esMayor = 5 > 3; // Resultado: true

Less than<

Check if one value is less than another.

let esMenor = 5 < 3; // Resultado: false

Greater than or equal to>=

Check if one value is greater than or equal to another.

let esMayorOIgual = 5 >= 5; // Resultado: true

Less than or equal to<=

Check if one value is less than or equal to another.

let esMenorOIgual = 5 <= 3; // Resultado: false

Logical Operators

Logical operators are used to combine or invert Boolean values. The most common are:

AND&&

Returnstrueif both operands are true.

let resultadoAnd = 5 > 3 && 8 > 5; // Resultado: true

OR||

Returnstrueif at least one of the operands is true.

let resultadoOr = 5 > 3 || 2 > 5; // Resultado: true

NOT!

Inverts the Boolean value of an operand.

let resultadoNot = !(5 > 3); // Resultado: false

Assignment Operators

Assignment operators are used to assign values to variables. The most common are:

Simple Assignment=

Assign a value to a variable.

let x = 5; // Asigna 5 a x

Assignment of sum+=

Add a value to a variable and assign the result to that variable.

let y = 5;
y += 3; // Equivalente a y = y + 3; Resultado: 8

Subtraction Assignment-=

Subtract a value from a variable and assign the result to that variable.

let z = 5;
z -= 2; // Equivalente a z = z - 2; Resultado: 3

Multiplication Assignment*=

Multiply a variable by a value and assign the result to that variable.

let a = 5;
a *= 2; // Equivalente a a = a * 2; Resultado: 10

Division Assignment/=

Divide a variable by a value and assign the result to that variable.

let b = 10;
b /= 2; // Equivalente a b = b / 2; Resultado: 5

Module Assignment%=

Apply the modulo operator to a variable and assign the result to that variable.

let c = 10;
c %= 3; // Equivalente a c = c % 3; Resultado: 1

Exponentiation Assignment**=

Raise a variable to a power and assign the result to that variable.

let d = 2;
d **= 3; // Equivalente a d = d ** 3; Resultado: 8

String Operators

String operators are used to manipulate text. The most common is the concatenation operator.+Concatenation, which joins two or more strings.

Concatenation+

let saludo = "Hola, " + "mundo!"; // Resultado: "Hola, mundo!"

Summary

Operators in JavaScript are fundamental for performing mathematical operations, comparisons, logical combinations, and value assignments. Knowing and understanding these operators will allow you to manipulate data and control the flow of your code effectively. Practice with simple examples to familiarize yourself with their use and behavior.

Exercises

💻Try these basic JavaScript exercises: