A Short trip to JavaScript

1. Truthy and Falsy Value

Sakib Noman
2 min readMay 8, 2021

We work with value in programming as well as JavaScript. Some value bear falsy and some value truthy. null, undefined, [], {}, 0, “” bear falsy value. On the other hand, [“something”], {foo: “bar”}, 1, ‘Hello’ etc. bear truthy value.

2. Null vs Undefined

Though Null and Undefined are falsy values, they have differences. A value that is not initialized is undefined. On the other side, Null is initialized manually.

3. Double equal(==) vs Triple equal(===)

JavaScript has Double equal and Triple equal to compare variables. Double equal can not check the type of variable. Triple equal can also check the type of variable.

Now See some coding example

2. Find the largest element of an array:
We can easily find the largest element of an array using simple logic. At first, declare a variable to store the largest value and initialize first element of array. Then use a loop to traverse the array. If we found largest then initialized previously, we have to initialize current value.

let myArr = [1,4,6,3,9,21]
let max = myArr[0]
for(let v of myArr){
if(v>=max){
max=v
}
}

2. Remove duplicate from array:
The logic is so simple. We can use a function findIndexOf()

let myArr = [1,1,2,3,3,2]
let newArr = []
for(let v of myArr){
if(newArr.indexOf(v) === -1 ){
newArr.push(v)
}
}
console.log(newArr) //[ 1, 2, 3 ]

3. Count number of words in string:

We can count number of space in the string and simply increase 1 of the count because one word is possible without space.

let str = “Hello, JavaScript I will Win You”
let count = 1
if(str.length == 0){
count = 0
}
else{
for(let i = 0; i<str.length;i++){
if(str[i]==’ ‘)
{
count++
}
}
}
console.log(count)

--

--