operators' foundations are essential for performing mathematical operations, logical comparisons, data manipulation and flow control within a program. Let's learn them using javascript ?
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)
Attribution operators are used to assign values to variables. The most common operator is "=", but there are combinations with arithmetic operators that facilitate code.
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 value boolean ( True or false ). They are widely used in control structures, such as if , and while .
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(num14. Logic operators
Logic operators are used to combine boolean (true or false) expressions and are essential for flow control.
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')
let contador = 5; contador ; // Incrementa: contador = 6 console.log(contador); // Saída: 6 contador--; // Decrementa: contador = 5 console.log(contador); // Saída: 5
6.
Ternary operatorsExample:
let idade = 18; let status = (idade >= 18) ? 'Maior de idade' : 'Menor de idade'; console.log(status); // Saída: 'Maior de idade'
7.
Concatenar Strings ()(join texts). Example:
let primeiroNome = "Maria"; let segundoNome = "Silva"; let nomeCompleto = primeiroNome " " segundoNome; console.log(nomeCompleto); // Saída: "Maria Silva"Bitwise operators (bits a bits)
let x = 5; // Binário: 0101 let y = 3; // Binário: 0011 console.log(x & y); // AND Bit a Bit: 1 (0101 & 0011 -> 0001) console.log(x | y); // OR Bit a Bit: 7 (0101 | 0011 -> 0111)
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3