DEV Community

Rayane Pimentel
Rayane Pimentel

Posted on

1 1

Parte I.II Operadores

Operadores


Operadores Aritméticos

São os operadores que já conhecemos:

  • [+] adição - adiciona números
let a = 10;
let b = 3;
let soma = a + b; //13
Enter fullscreen mode Exit fullscreen mode
  • [-] subtração - subtrai números
let a = 10;
let b = 3;
let subtrai = a - b; //7
Enter fullscreen mode Exit fullscreen mode
  • [*] multiplicação - multiplica números
let a = 10;
let b = 3;
let multiplica = a * b; //30
Enter fullscreen mode Exit fullscreen mode
  • [/] divisão - divide números
let a = 10;
let b = 5;
let divide = a / b; //2
Enter fullscreen mode Exit fullscreen mode
  • [%] modulo - devolve o resto da divisão.
let a = 10;
let b = 4;
let resto = a % b; //2
Enter fullscreen mode Exit fullscreen mode
  • [++] incremento - incrementa números.
let a = 10;
a++;
let b = a; //11
Enter fullscreen mode Exit fullscreen mode
  • [--] decremento - diminui números.
let a = 10;
a--;
let b = a; //9
Enter fullscreen mode Exit fullscreen mode
  • [**] exponenciação - calcula a base elevada a potência do expoente
let a = 2;
let b = 3;
let resultado = 2 ** 3; //8
Enter fullscreen mode Exit fullscreen mode

Operadores de Comparação

Faz uma comparação entre valores e/ tipos e retorna true ou false.
É como se vocês estivesse perguntando:

isso é `operador de comparação` a aquilo?
- Sim (true)
ou
- Não (false}
Enter fullscreen mode Exit fullscreen mode
  • [>] maior ou [<] menor
let a = 10;
let b = 5;

let conferir = a > b; // true
let conferirNovamente = a < b; //false
Enter fullscreen mode Exit fullscreen mode
  • [>=] maior ou igual ou [<=] menor ou igual
let a = 10;
let b = 5;
let c = "10"

let conferir = a >= b; // true
let conferirNovamente = a <= b; //false
let resultado = a >= c; //true
let porqueEIgual = a <= c; //true
Enter fullscreen mode Exit fullscreen mode
  • [==] igualdade - compara se os valores são iguais(true)
let a = 10;
let b = 5;
let c = "10";

let conferir = a == b; //false
let conferirNovamente = a == c; //true
Enter fullscreen mode Exit fullscreen mode
  • [!=] compara se os valores são diferentes
let a = 10;
let b = 5;
let c = "10";

let conferir = a != b; //true
let conferirNovamente = a != c; //false
Enter fullscreen mode Exit fullscreen mode
  • [===] igualdade - compara se os valores e os tipos são iguais
let a = 10; //tipo number e valor 10
let b = 5; //tipo number e valor 5
let c = "10" //tipo string e valor 10
let d = 5; //tipo number e valor 5

let conferir = a === b; //false
//a e b são do tipo number, mas valores diferentes
let conferirNovamente = a === c; //false
// a e c são tipos diferentes, mas com o mesmo valores
let resultado = b === d; //true
// b e c são do mesmo tipo e tem o mesmo valor
Enter fullscreen mode Exit fullscreen mode
  • [!==] diferente - compara se os valores ou os tipos são diferentes
let a = 10; //tipo number e valor 10
let b = 5; //tipo number e valor 5
let c = "10" //tipo string e valor 10
let d = 5; //tipo number e valor 5

let conferir = a !== b; //true
//a e b são do tipo number, mas valores diferentes
let conferirNovamente = a !== c; //true
// a e c são tipos diferentes, mas com o mesmo valores
let resultado = b !== d; //false
// b e c são do mesmo tipo e tem o mesmo valor
Enter fullscreen mode Exit fullscreen mode

Exercício

  • Como vimos podemos fazer várias comparações. O que você acha que acontecerá se comparamos o menor ou o maior utilizando string?
"teste" > "javascript";
"abc" < "def";
Enter fullscreen mode Exit fullscreen mode

Se você não entendeu o que aconteceu, veja essa explicação
Explicação

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay