0

Function 1 and Function 2 both are the same but with a different syntax. The thing is that when I call function 2 (arrow function syntax) in isItAPalindrome function it gives me a "is not defined" error. But when I declared function 2 within isItAPalindrome function it does not give me any error.

Function 1:

function reverseStr(str){
    return str.toLowerCase().split('').reverse().join('');
}

Function 2:

const reverseStr = (str) => str.toLowerCase().split('').reverse().join('');

When I call the function 1 it works the way it's supposed to do.

 function isItAPalindrome(str) {

//When I call the arrow function here it works.
//const reverseStr = (str) => str.toLowerCase().split('').reverse().join('');

if (str.toLowerCase() == reverseStr(str)) {
    return "It's a palindrome!";
} else {
    return "It's not a palindrome!";
}
}

console.log(isItAPalindrome("sum mus"));

Why does this happen with the arrow functions in?

  • 5
    You're probably seeing hoisting. Where is the variable declared? – SLaks Feb 04 '18 at 20:58
  • ... or block scoping – Jonas Wilms Feb 04 '18 at 21:22
  • probable duplicate of [`const functionName = () => {}` vs `function functionName() {}`](https://stackoverflow.com/q/336859/1048572) – Bergi Feb 04 '18 at 21:59
  • @SLaks they are declared outside of the function isItAPalindrome, in the global scope. –  Feb 04 '18 at 23:41
  • @Bergi When I run the code I comment one of them, firstly I declared only the arrow function and it gave me that error, so I declared the same function with the normal syntax to see what happens and apparently arrow functions behave differently from normal functions. –  Feb 04 '18 at 23:48
  • @Yadiel Arrow functions [are different than normal functions](https://stackoverflow.com/q/34361379/1048572), but it those differences do not matter in the example you made. Please post a snippet with your complete non-working code – Bergi Feb 05 '18 at 13:19

0 Answers0