Difference Between let and const for Arrow Function Declarations

I’m working through the freeCodeCamp Full Stack Developer JS Workshop Calculator project, and in Step 18, I wrote this arrow function:
let calculateSquareRoot = (num) => Math.sqrt(num);

It worked fine, but they use const in the lecture videos (And, I also used it in previous steps…), like this:
const calculateSquareRoot = (num) => Math.sqrt(num);

Both versions seem to work, but I’m wondering if there’s any practical difference between using let and const when declaring arrow functions. Are there specific cases where one is better than the other? For instance, does it matter if I might want to reassign the function later, or are there best practices I should follow when choosing between let and const for arrow functions?

Thank you, in advance…!

I think it doesn’t apply solely to the arrow functions, but generally - use const, unless you specifically want to reassign that variable later. Reassigning of function sounds like something that should be avoided.

Keep in mind const doesn’t prevent mutating, so ie. declaring array, which will be later mutated - filled with items, with const will still work fine.

1 Like

Thank you for replying…! I was curious about it. That’s why I asked the question, to know if there are any reasons behind using const, instead of using let in many places.

I could find a place to use let, instead of using const. :slightly_smiling_face:

(The code snippet is messy and less readable. But, I wanted to make it messier intentionally to check if this works. :face_with_peeking_eye: Sorry…!)

With let;

let year = prompt("Enter a year...", "2025");
let isLeapYear = (year) => (isLeapYear = (((Number(year%400) === 0) & (Number(year%100) === 0)) | ((Number(year%4) === 0) & (Number(year%100) !== 0))) ? `${year} is a leap year.` : `${year} is not a leap year.`);
console.log(isLeapYear(year));

Code works fine…!

With const;

let year = prompt("Enter a year...", "2025");
const isLeapYear = (year) => (isLeapYear = (((Number(year%400) === 0) & (Number(year%100) === 0)) | ((Number(year%4) === 0) & (Number(year%100) !== 0))) ? `${year} is a leap year.` : `${year} is not a leap year.`);
console.log(isLeapYear(year));

Gives this error: :x: TypeError {“Assignment to constant variable.”}

Could you explain what’s the goal of this construct? After such function runs once, it’s no longer is a function.

1 Like


It works as a function…! So, Technically, It is a function…! It is an arrow function with a ternary if statement inside of it.

I can call it anytime like a normal function…!

Goal of it: I thought that, It looks so cool If I can put an entire function in one line while doing this, although it ruined the readability.

Hmm, this doesn’t look right. Where are you running it?

Problem isn’t with using ternary operator. In function, to the isLeapYear is assigned ((isLeapYear = (((Number(year%400) (...)) result of the first calculation of the leap year.