Open In App

Code Golfing in JavaScript

Code Golf in JavaScript refers to attempting a problem to solve using the least amount of characters possible. Like in Golf, the low score wins, the fewest amount of characters “wins”. JavaScript is a fantastic language for code golfing due to backward compatibility, quirks, it is being a high-level language, and all the coercion. So, here we will look at some Code Golfing techniques in JavaScript language:


1. Checks if a variable is equal to some positive no: We can simply do this with a bunch of if-else statements, but we have different methods also like subtracting that positive integer from the number and checks whether it is greater than zero or not.

2. Checking the case of literals: We can even compare the literal with {} for checking its case, it returns true for the uppercase and false for the lowercase.



3. Floor the value: A straight forward solution is used in floor function present in the Math library but it takes little more characters. We can do the same in a few characters by using the | operator.

4. Ceil value: A straight forward solution is to use ceil function present in the Math library but it takes little more characters. We can do the same in a few characters by using the combination of %, ~ operator.



5. Rounding the value: A straight forward solution is used to round function present in the Math library but it takes little more characters. We can do the same in a few characters by using the combination of |, + operator.

6. Arrow functions: Arrow functions provides a concise way to write functions in the JavaScript.

7. Alternatives for min function: Ternary operator always saves bytes. We can compare two numbers using the ternary operator.

8. Alternatives for max function: Ternary operator always saves bytes. We can compare two numbers using the ternary operator.

9. Absolute value: A straight forward solution is to use absolute function present in the Math library but it takes little more characters. We can do the same in a few characters by using the ternary operator.

10. Save bytes in Loops: We can save bytes by changing the variable on the last time used.

11. Calculating Sum or Product of Array: We have the option to do task by iterating the loop but for saving bytes we can use eval and arithmetic operators like +, *, ^etc.

12. Check for NaN: NaN method in JavaScript is used to determines whether the passed value is NaN(Not a Number) and is of the type “Number”.

Note: It only works if typeof(x) == “number”

13. for vs while loops: Both loops are efficient in terms of time complexity but when it comes to space complexity, many times for loop outbeats while loop.

14. Combining Nested loops into a single loop: We can combine the nested loop of 2 or more degree into a single loop.

15. Generating Random numbers between the range: We can use the Date class to generate the random number because the Date is stored internally in JavaScript as the number of milliseconds since an epoch.

Article Tags :