Using Declarations and If statements in a Function

Hello. The following code gives me a SyntaxError: Unexpected token {
I don’t understand why. Thank you for your help.

const compareGuesses = (userGuess, compGuess, targetNum) => {
  const userGuessDiff = Math.abs(targetNum - userGuess) ;
  const compGuessDiff = Math.abs(targetNum - compGuess) ;
  If (userGuessDiff <= compGuessDiff) {
    return true ;
  } else {
    return false ;
  } ;

Number Guesser | Codecademy

Hello, and welcome to the forums!

This is one of those times where the error message is a symptom of another problem. The error message should have given a line number so you’d know which of the { it was complaining about. From there you can sometimes work backwards to figure out what’s going on

In this case, it should be complaining about your { on the line with you if conditional because you have a typo:

If (userGuessDiff <= compGuessDiff) {

Because you used an uppercase i, JavaScript thought you were trying to call a function named If with the argument that is either true or false, and it got confused by the extra curly brace after it so it threw the error.

1 Like

Thank you very much @selectall. :slight_smile:
Syntax errors are going to be the death of me!