Random stuff in javascript — part 1
Hello everybody!, today I’m gonna be sharing random stuff. Sort of little tricks and general stuff that sometimes looks esoteric to newbies when they view javascript open-source libraries.
Let’s jump all in !!
The first I’m gonna be talking about is

Suppose we have two variables; typedItem and items declared and assigned with values
const typedItem = "Alf"
const items = ["Alfred","Alvin", "Alan", "John", "Doe"]And we want to be able to find items in the items’ list that contains or matches the typedItem’s value. We ideally would use a filter with the triple equals for the comparison.
ie.
const filteredList = items.filter((item) => item.indexOf(typedItem) === -1)A shorter approach or little trick to achieving the same result is by eliminating the triple equals and the negative one and having a tilde in front of the item.indexOf.
const filteredList= items.filter((item) => ~item.indexOf(typedItem))To understand this better open to your browser console and console.log( ~1, ~0, ~5, ~-1)
You realize that tilde adds 1 to the number in question and negates the result, hence the respective values -2, -1, -6, 0.
In javascript any number, positive or negative is true with the exception of zero. That is to say that -1, -5, 8, 0.11, … all have true to be their boolean value but 0’s boolean value is false.
The way in which we can see this in action is by typecasting. This can be done by applying two exclamation marks.
!!whatevernumberTry the following in your web console
console.log(!!-1)//true
console.log(!!0)//false
console.log(!!5)//true
console.log(!!0.11)//trueBack to our code, indexOf returns -1 when typedItem does not match item and otherwise returns a value which is not -1
item.indexOf(typedItem)
//returns -1 if typedItem doesn't matches itemTilde of the value -1 gives 0, you remember ? 🤔 From our tilde calculation add one and negate the result. Negative of zero is still zero. Do you also remember the boolean value of zero ? Well, it’s false.
So then the expression
~item.indexOf(typedItem)returns 0 which implies false when typedItem is not found in or does not match string item.
Now, if the typedItem is found in the string item indexOf will return a value which isn’t negative one. Applying the tilde to the value will give a non-zero value which implies true per the fact that any non-zero digit’s boolean value in javascript is true.
Up on to now, I’ve being assuming you already know what filter does. The Filter function creates a new array with all elements that passes the test implemented by the provided function. In our case filter will create new array called filteredList with all elements that returns true for
~item.indexOf(typedItem)Next, let talk about eval.
Eval is a function that developers seldomly use but may come in handy when you have to execute javascript code presented as string. The example below makes the above point clear.
console.log(eval("var john={name: 'John Doe', age: 2}"))//console log john and note your observation
console.log(john)
Clearly we see that eval evaluates the JavaScript code represented as string and we are able to log john.
It is worth noting that changing var to let or const changes the scope of execution, which implies that the variable john won’t be available for you to log on the next line. I will leave that for you to try.
Next on our list is Object destructuring for array items
I suppose object destructuring and array destructuring isn’t new to you. I won’t talk about that but rather I will talk about destructuring an array in object form.
This is better explained using the code below.
const myList = ["Alfred", 28, "University of Ghana", "computer science economics statistics and mathematics"];
const { 2: university, 1: age } = myList;
//log university and age to see what it has
console.log(university) //University of Ghana
console.log(age) // 28As you can see, the array indexes have been mapped with declared variables university and age and we can easily get their values by referencing them(university and age).
I will stop here because I don’t want this post to be lengthy. I will continue another time with the part 2 of random stuff. Let me know if you have any random stuff you want me to talk about 😊. You can send me an email at alfredayibonte@gmail.com or tweet at me at alfredayibonte
