"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Fundamentals of Operators

Fundamentals of Operators

Published on 2025-02-04
Browse:287

Fundamentos de Operadores

operators' foundations are essential for performing mathematical operations, logical comparisons, data manipulation and flow control within a program. Let's learn them using javascript ?

Main types of javascript operators:

1. Arithmetic operators

are used to perform mathematical operations between numbers. These operators include:

  • Addition () : Add two values.
  • Subtraction (-) : subtract the second value of the first.
  • Multiplication (*) : multiplies two values.
  • Division (/) : divides the first value by the second.
  • module (%) : returns the rest of the division between two values.
  • Exponence (``) **: Raises the first value to the power of the second.

Example:

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)

2. Attribution operators

Attribution operators are used to assign values ​​to variables. The most common operator is "=", but there are combinations with arithmetic operators that facilitate code.

  • ATTRIBUTION (=) : attributes a value to the variable.
  • attribution with addition (=) : sum and attributes the result to the variable.
  • ATTRACT WITH SUBTRATION (-=) : subtract and attributes the result to the variable.
  • ATTRIBUTION WITH MULTIPLATION (*=) : multiplies and attributes the result to the variable.
  • ATTRIBUTE WITH DIVISION (/=) : divides and attributes the result to the variable.

Example:

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

3. Comparison operators

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 .

  • Equality (==) : check if the values ​​are the same, without checking the type.
  • Identity (===) : check if the values ​​and types are exactly the same.
  • different (! =) : check if the values ​​are different.
  • strict difference (! ==) : check if the values ​​and types are different.
  • greater than (>) : check if the left value is higher.
  • smaller than (: check if the left value is lower.
  • greater or equal (> =) : check if the left value is greater or equal.
  • smaller or equal (: check if the left value is smaller or equal.

Example:

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 



4. Logic operators

Logic operators are used to combine boolean (true or false) expressions and are essential for flow control.

  • and (&&) : Returns True if both expressions are true.
  • or (||) : Returns True if at least one of the expressions is true.
  • not (!) : reverses the boolean value of an expression.

Example:

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')

5. These operators work with just one operating and can modify or return the value of a variable.

    Increation ()
  • : adds 1 to the value of the variable.
  • Decrement (-)
  • : subtract 1 of the value of the variable.
  • Example:

Let accountant = 5; Accountant; // increments: accountant = 6 console.log (accountant); // Output: 6 counter--; // Decrement: Accountant = 5 console.log (accountant); // Output: 5

let contador = 5;

contador  ;  // Incrementa: contador = 6
console.log(contador);  // Saída: 6

contador--;  // Decrementa: contador = 5
console.log(contador);  // Saída: 5

    prefixed x or -x:
  • uses the current value of the variable in the expression and then makes the increase/decrement.
  • Posfixed x or x-:
  • increases/decrees the value before using it in the expression (as exemplified before).
  • Learn more by clicking here

6.

Ternary operators

The ternary operator is a simplified form of an IF to assign values ​​based on a condition. Is your structure a condition? Value_SE_VERDEIRO: VALUE_SE_FALSO.

Example:

Let's age = 18; Let status = (age> = 18)? 'Upper age': 'Understanding'; console.log (status); // Output: 'Upper age'

let idade = 18;
let status = (idade >= 18) ? 'Maior de idade' : 'Menor de idade';

console.log(status);  // Saída: 'Maior de idade'

7.

Concatenar Strings ()

The addition operator () can also be used to

concatenate strings

(join texts). Example:

let first name = "Maria"; let secondname = "Silva"; Let Name Complete = First Name "Second Running; console.log (namecomplete); // Exit: "Maria Silva"

let primeiroNome = "Maria";
let segundoNome = "Silva";

let nomeCompleto = primeiroNome   " "   segundoNome;
console.log(nomeCompleto);  // Saída: "Maria Silva"
Bitwise operators (bits a bits)

These operators perform bits -level operations (0s and 1s), usually used in low level programming, such as hardware operations. It is not common to use these types of operators.

    and bit a bit (&)
  • or bit a bit (|)
  • xor bit a bit (^)
  • not bit a bit (~)
  • displacement on the left ( Right displacement (>>)
  • Example:

Let x = 5; // Binary: 0101 Let y = 3; // Binary: 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)

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)

Release Statement This article is reproduced in: https://dev.to/laisdiasdev/fundamentos-de-opradores-34op?
Latest tutorial More>

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