Home > OS >  I struggle to figure out the reason why I got SyntaxError: Unexpected token '{' in node.js
I struggle to figure out the reason why I got SyntaxError: Unexpected token '{' in node.js

Time:05-05

I am learning basic Javascript now but am stuck in this simple error because of syntax-error.

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];
let a;
let b;
// let C;
rl.on('line', (line) => {

  input = line.split(' ');
  
}).on('close', () => {
  
  a = input[0];
  b = input[1];
  
  if (a < b) {
    console.log('<');
  }else if (a > b) {
    console.log('>');
  }else (a == b) {
    console.log('=');
  };
});

// 1 2

CodePudding user response:

In the else statement you cannot put the condition. Replace your code with the below code.

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = [];
let a;
let b;
// let C;
rl.on('line', (line) => {

  input = line.split(' ');
  
}).on('close', () => {
  
  a = input[0];
  b = input[1];
  
  if (a < b) {
    console.log('<');
  }else if (a > b) {
    console.log('>');
  }else {
    console.log('=');
  };
});

Hope, it helps!! XD

  • Related