Which one to use | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Which one to use

I've made a JavaScript file in which I made some kind of library to make some functions be easier. My question is: Is it better to use native JavaScript functions or use constants, so that no variable can have the same name? What are the pros and cons for both native js and constants If you want I can include the code

26th Dec 2018, 12:41 AM
Roel
Roel - avatar
5 Answers
+ 2
Both can be implemented well. If you use closure, there would not be any name conflict between your library code with other codes. All sealed by closure, you only export some public variables.
26th Dec 2018, 2:51 AM
Calviղ
Calviղ - avatar
+ 1
Run you library codes in closure, to ensure code isolated from the main JavaScript.
26th Dec 2018, 1:57 AM
Calviղ
Calviղ - avatar
+ 1
Thanks for the tip, but should I use functions or constants in that code. Like this: is it better to do this function Shuffle(input){ return input.sort(function(){return Math.random()-0.5}); } To be able to name variables like this function Or this const Shuffle = input => { return input.sort(const = () => {return Math.random()-0.5}) } To make sure no other variable can be named like this
26th Dec 2018, 2:02 AM
Roel
Roel - avatar
+ 1
Thanks!
26th Dec 2018, 3:07 AM
Roel
Roel - avatar
0
By the way, besides the functions I also have some variables that are constant, like PI. Should I let them be var or const? (Pros and cons?)
26th Dec 2018, 12:44 AM
Roel
Roel - avatar