‘Zero’! Truthy or Falsy?
Think about a number. Is ‘zero’ a truthy value? Generally, you can say zero is a number and as a number, it should be truthy. But you're wrong. Zero is a falsy value. For example:
Why? because zero means zero and zero means zero. Just kidding, zero have nothing. Well, you know what I’m saying.
Now, const x = ‘0’; is truthy or falsy?? You may say, heheheh! it’s falsy. Nope brother, again you're wrong. When you put something in quote sign (‘’) it's no more false. Because it’s a string now. You will find it out in the next section.
“‘ vs “ “
First, let me show you a code example:
Now, it should be understood to you why “0” is not falsy. Because typeof(0) is a number but typeof(“0”) is a string. And a string with a value cannot be false.
Some other Falsy Values
- undefined: undefined is a falsy value. When you declare a variable but didn’t set any value or forgot to declare then it’ll be undefined. Not only that, you may get undefined in many other ways. We’ll talk about them too later.
- null: null is also a falsy value as undefined. Null means empty and an empty variable cannot explain anything, so null is falsy too.
- NaN: NaN stands for ‘Not a Number’. Sometimes you'll find this kind of error while working with numbers. NaN is a falsy value.
- False: false itself a falsy value. Because false is false. It’s a boolean type variable. I think no more explanation needed. But if you put false inside quotes like zero inside quotes it’ll be truthy. Because it’ll be string then, no more boolean type.
undefined vs null
- If you declare a variable without value and try to read it’s value then you'll get undefined;
- If you call a function in which there is no return statement in it you’ll get undefined;
- If you call a function that has one or more parameters but you don’t put any parameter or forgot to put then you’ll get undefined;
- If you try to read a property of an object or array but the proper is not declared you’ll get undefined;
- If you're a monkey type developer and declare a variable with property undefined like so, const a = undefined; you'll get undefined;
- The only way to get null in return is to declare a variable with the null property;
These are not only reasons for getting undefined. There are some others. You’ll learn them automatically I think if you do programming regularly…
Magic between ‘==’ and ‘===’
Before explaining, I wanna show you some code:
As you can see, 10 = ‘10’ is true, you may ask why? how? because while checking condition here I used ‘==’. The reason is if you use ‘==’ it will just check for value, not the type. You can say less equal (‘==’) means less strict. And when I used ‘===’ it shows false. Because beside values it checked type of the variables. So, more equal (‘===’), more strict.
But why 0 and false are true while checking with == and false when === are used. Tricky things are happening here. 0 is a falsy value, as well as false, is a falsy value. This is why when it checked less strictly 0 and false are equal I mean falsy. But when checked with === they're caught because 0 is a number and false is a boolean. The same thing happens with e and x I mean 1 and true.
map find filter
2. filter: filter is also like map but it has its own functionality. You can filter out some data from an array using filter method as its name refers;
3. find: find is almost similar to filter. But instead of returning a whole list it returns only one element. It will be clear to you after seeing an example:
But if you put a variable inside a function and try to access it from outside you cannot. Because you cannot access variables from block-level elements. Like it,
It doesn't work because ‘a’ is declared inside a function. This function is its scope means playground. But in the first example, ‘x’ was declared outside functions so it's a global variable. You can access it from anywhere.
But the scenario is a little bit strange for those variables that are declared with ‘var’ keywords. If you use var keywords inside or outside the block-level element it will be a global variable. And you can access them from anywhere.
Duplicate Numbers Removal
In this example, you see there is a lot of duplicates. But I remove them? If you don’t understand let me make it clear to you. I have used a for loop. In this loop, I am reading all elements of this array. After reading I used an if conditional. This is the key to this problem-solving. I’m checking the index of the accessed number in the noDuplicate variable. If there is no duplicate the condition will be true and this number will be pushed to noDuplicate. Otherwise, nothing will happen. See, how easy programming is!!
Call and Apply
First I wanna show you something, just see and try to relate with the example provided up:
I think you understand. It is almost the same as bind. But a few differences are noticeable. First, I want to talk about the main difference between bind and call, apply. In Bind, you need to call it first then pass parameters to see the outcome. But in other methods, it’s not necessary compared to bind keyword. Now, what's the difference between call and apply? in the call method you have to pass parameters using a comma. And in the apply method, you have to use an array just like the example shown above.