Hi, I'm Aasim Ashraf 👋. This is my first post
Today, we'll explore an interesting quirk in JavaScript regarding the use of the arguments
object and function parameters.
Consider the following code:
Guessing the Output
If you guessed 3
, you're Wrong! ❌ The output is 5
✅. The arguments
object is a local variable
available within all non-arrow functions
. If you change the parameters
values, it will also change the arguments
value and vice versa.
Solutions to Fix It?
- Strict Mode: In strict mode (
"use strict";
), the link betweenarguments
andparameters
is broken entirely.
- Destructring: You can use
destructuring
to avoid this issue.
- Default parameters values: You can also use
default parameters
to avoid this issue.
- Rest Parameters: You can also use
rest parameters
to avoid this issue.
Conclusion
The arguments
object is a powerful tool in JavaScript, but it can lead to unexpected behavior if not used carefully. By understanding how it interacts with function parameters, you can avoid common pitfalls and write more robust code. Happy Coding! 🚀
I hope you found this post helpful. Thanks for reading! 👋