Back to Help Center

CONDICIONAL : IF - JS

Equipe Learning

Testes em valores vazios / não vazios

Testes em valores não nulos

/*
 Se não for nulo, podemos retornar true
*/

const myVariable = msg.payload.myVariable;
let booleanNotNull;

if (myVariable){
    booleanNotNull = true ;
}

Testes em valores não vazios

/*
 Se for vazio, podemos retornar uma string vazia 
*/

const myVariable = msg.payload.myVariable;
let booleanNotEmpty;

if (myVariable != ""){
    booleanNotEmpty = true;
}

Testes com números

const var1 = 2;
const var2 = 3;

// IGUALDADE
if (var1 == var2){  // FALSE
    //...
}

// DESIGUALDADE
if (var1 != var){ // TRUE
    //...
}

// ESTRITAMENTE SUPERIOR
if (var1 > var){ // FALSE
    //...
}

// SUPERIOR OU IGUAL
if (var1 > var){ // FALSE
    //...
}

// INFERIOR OU IGUAL
if (var1 <= var){ // TRUE
    //...
}

Testes com strings

const string1 = "this is my string"
const string2 = "this is a string"

// IGUALDADE
if (string1 == string2) { // FALSE
    //...
}

// DESIGUALDADE
if (string1 != string2) { // TRUE
    //...
}

Operandos

/*
    AND
*/
if (var1 == var2 && var2 == var3){
    //....
}

/*
    OR
*/
if (var1 == var2 || var2 == var3){
    //....
}

Switch

const expr = 'Papayas';
switch (expr) {
  case 'Oranges':
    console.log('Oranges are $0.59 a pound.');
    break;
  case 'Mangoes':
  case 'Papayas':
    console.log('Mangoes and papayas are $2.79 a pound.');
    // resultado esperado: "Mangoes and papayas are $2.79 a pound."
    break;
  default:
    console.log(`Sorry, we are out of ${expr}.`);
}
Powered by Zendesk