JavaScript Curly Braces Gotcha: Explicit Return Required

Don't let curly braces trick you in JavaScript! 🚀 I came across this tricky interview question today and it’s a perfect reminder of how small syntax details change everything. Many developers would glance at this and expect [2, 4, 6]. But if you run this code, the result is actually [undefined, undefined, undefined]. Why? In JavaScript arrow functions, if you use curly braces { }, you must use an explicit return keyword. Without it, the function returns undefined by default for every element in the map. Fix it by: 1. Adding the return keyword inside the block. 2. Or, removing the curly braces for an implicit return: nums.map(num => num * 2). Small details make big differences in debugging! 💻 Feel free to reach me out for any career mentoring. Naveen .G.R | CareerByteCode #javascript #mernstack #webdevelopment #codingtips #learningjourney #60daychallenge

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories