Let’s practice some JavaScript
In this article we will cover 10 common topics in JavaScript. So let’s get started.
1. Truthy and falsy values
A value is considered truthy if it’s coerced to true in a Boolean context and other wise it’s a falsy value.
There is handful of falsy values. And rest are truthy values. There are 8 false values:
false
0
-0
0n
""
null
undefined
NaN
2. Closure
Closure is the fact that functions encloses their surrounding environment. That is a function not only have access to it’s local scope but also the outer scope where it was created(not where it were called).
Example:
let name = “Ashutosh”;
function greet() {
console.log(‘hello ‘ + name);
}greet();
Above the greet function encloses the outer scope. It is most simplest example of closure. It’s so simple, that some people don’t even consider this as closure, but it actually is. What they usually expect is the following answer:
A nested function that is returned and have access to it’s environment.
3. bind, call and apply
These are 3 methods of functions.
3.1 bind
Syntax:
const boundFunc = someFunction.bind(object, arg1, arg2, ...)
Bind returns a new function by setting it’s this
to object
and binding the rest of the arguments.
3.2 call
call
is for calling a function by providing it a specific this
value. Syntax:
someFunc.call(object, arg1, arg2, ..)
The above code calls someFunc
with object
as it’s this
and arg1
and arg2
etc as arguments.
3.3 apply
This similar to call
except that you need to provide the arguments in an array. Example:
Math.max.apply(null, [1,2,3,4,5]); // 5
4. What is recursion?
Recursion is means calling a function inside itself. Problems that have that can be divided into sub problems with the same structures are the perfect candidate for applying recursion. This kind of problems are easy to solve using recursion and hard with iterative approach.
Recursion is about 3 times slower than iterative approach. But this lack of efficiency is big issue most of the time, cause recursive code is usually much more clean and simpler than iterative code.
5. Finding factorial of a number
It can be done with recursion very easily:
const factorial = n => n ? n * factorial(n - 1) : 1;
You can use loop too if you wish.
6. How to remove duplicate items from an array
There are several approaches to it. Below is my solution:
const removeDuplicate = arr => {
const uniqueArray = [];
for (let i = 0; i < arr.length; i++) {
if (!uniqueArray.includes(arr[i])) uniqueArray.push(arr[i]);
}
return uniqueArray;
}
7. Reverse a string
Let’s be lazy and solve it with recursion:
const reverseString = str => str ? str.substr(-1) + reverseString(str.slice(0, str.length - 1)) : '';
8. Finding Fibonacci series
Let’s be super lazy and solve it with recursion:
const fibSeries = n => n < 2n ? [[0n], [0n, 1n]][n] : (s => [...s, s.pop() + s.pop()])(fibSeries(n - 1n));console.log(fibSeries(500n).join("\n"));
I used bigint for displaying large numbers.
9. Counting the number words
Writing a program to count the number of words in some text. If we assume that that words are separated by white spaces then the solution is simple:
const numberOfWords = someText.split(/\s+/).length;
10. Checking if a number is prime or not
It can be checked with following function:
const isPrime = n => {
if (n < 2) return false;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
Note the use of Math.sqrt
.