The fundamentals of operators in programming are essential for performing mathematical operations, logical comparisons, data manipulation and flow control within a program. Let's learn them using JavaScript?
They are used to perform mathematical operations between numbers. These operators include:
let a = 10; let b = 3; console.log(a + b); // Adição: 13 console.log(a - b); // Subtração: 7 console.log(a * b); // Multiplicação: 30 console.log(a / b); // Divisão: 3.333 console.log(a % b); // Módulo: 1 (resto da divisão de 10 por 3) console.log(a ** b); // Exponenciação: 1000 (10 elevado a 3)
Assignment operators are used to assign values to variables. The most common operator is "=", but there are combinations with arithmetic operators that make the code easier.
let x = 5; x += 3; // x = x + 3 -> 8 x -= 2; // x = x - 2 -> 6 x *= 4; // x = x * 4 -> 24 x /= 2; // x = x / 2 -> 12 console.log(x); // Resultado final: 12
These operators compare two values and return a boolean value (true or false). They are widely used in control structures, such as if, and while.
let a = 10; let b = 3; console.log(a + b); // Adição: 13 console.log(a - b); // Subtração: 7 console.log(a * b); // Multiplicação: 30 console.log(a / b); // Divisão: 3.333 console.log(a % b); // Módulo: 1 (resto da divisão de 10 por 3) console.log(a ** b); // Exponenciação: 1000 (10 elevado a 3)
Logical operators are used to combine Boolean expressions (true or false) and are essential for flow control.
let x = 5; x += 3; // x = x + 3 -> 8 x -= 2; // x = x - 2 -> 6 x *= 4; // x = x * 4 -> 24 x /= 2; // x = x / 2 -> 12 console.log(x); // Resultado final: 125. Unary Operators
These operators work with just one operand and can modify or return the value of a variable.
let num1 = 10; let num2 = '10'; console.log(num1 == num2); // true (só compara o valor) console.log(num1 === num2); // false (compara valor e tipo) console.log(num1 != num2); // false (valores são iguais) console.log(num1 !== num2); // true (tipos são diferentes) console.log(num1 > 5); // true console.log(num1 <= 10); // true
It is important to remember that the order of these operators influences the behavior of the variable. There are two ways to use them:
Find out more by clicking here
The ternary operator is a simplified form of an if to assign values based on a condition. Is your structure a condition? value_if_true : value_if_false.
let a = true; let b = false; console.log(a && b); // false (AND: ambos devem ser verdadeiros) console.log(a || b); // true (OR: ao menos um deve ser verdadeiro) console.log(!a); // false (NOT: inverte o valor de 'a')
Learn more about Ternary Operators here
The addition operator ( ) can also be used to concatenate strings (join texts).
let a = 10; let b = 3; console.log(a + b); // Adição: 13 console.log(a - b); // Subtração: 7 console.log(a * b); // Multiplicação: 30 console.log(a / b); // Divisão: 3.333 console.log(a % b); // Módulo: 1 (resto da divisão de 10 por 3) console.log(a ** b); // Exponenciação: 1000 (10 elevado a 3)
These operators perform bit-level operations (0s and 1s), generally used in low-level programming, such as hardware operations. It is not common to use these types of operators.
let x = 5; x += 3; // x = x + 3 -> 8 x -= 2; // x = x - 2 -> 6 x *= 4; // x = x * 4 -> 24 x /= 2; // x = x / 2 -> 12 console.log(x); // Resultado final: 12
Understanding how Operators work is fundamental to building programs that perform calculations, comparisons and control the flow of code efficiently.
The above is the detailed content of Operator Fundamentals. For more information, please follow other related articles on the PHP Chinese website!