Javascript Important concepts

Md Abdullah Mamun
4 min readMay 8, 2021

truthy & falsy

javascript has truthy or falsy value. Javascript checks something like that. We try to find out know the truthy & falsy value through example.

var num = 4;
if(num > 0){
console.log('condition is true')// return 'condition is true'
}else{
console.log('condition is false')
}

But you can only declare just num variable name. The result will be the same.Example:

var num = 4;
if(num){
console.log('condition is true')// return 'condition is true'
}else{
console.log('condition is false')
}

Javascript takes in any number is true without num 0.Event number is -4 javascript takes it true value .If the number is 0 javascript will return false. Example

var num = 0;
if(num){
console.log('condition is true')
}else{
console.log('condition is false')// return 'condition is false'
}

Similarly in the case of a sting.Example :

1. var name = "string";
if(name.length > 0){
console.log('condition is true')// return 'condition is true'
}else{
console.log('condition is false')
}

2. var name = "string";
if(name.length){
console.log('condition is true')// return 'condition is true'
}else{
console.log('condition is false')
}
3. var name = " ";
if(name.length){
console.log('condition is true')// return 'condition is true'
}else{
console.log('condition is false')
}

look at the example above.First example where if name greater then 0 it will return 0 otherwise it returns false. But the second example removes greater then condition but it returns true. Antoher example.

2. var name = " ";
if(name.length){
console.log('condition is true')// return 'condition is true'
}else{
console.log('condition is false')
}
3. var name = "";
if(name.length){
console.log('condition is true')
}else{
console.log('condition is false') // return 'condition is false'
}

If anything has a string without an empty string it will return 0. But If space is given in an empty string it will be return true.That means Javascript checks something like that.

If anything has a string without an empty string it will return 0..But That means Javascript checks something like that

Tuthy value 
1.'0' (a string containing a single zero)
2.'false' (a string containing the text “false”)
3.[] (an empty array)
4.{} (an empty object)
5.function(){} (an “empty” function)
6. true

Falsy value
1.false
2. 0
3. ""
4.udenfined//
5.null
6.NaN

Null Vs Undefined

Undefined means anything is not defined.If you write a function and you forgot to return it will show undefined. Another reason is you have written a return but do not write what will be returned then it will be show undefined. Other is you direct set value is undefined. Another is you declare variable name but not set value then show undefined. example

1.function add(num1, num2)
{ console.log(num1 + num2);
return
}
console.log(1,2) //return undefined
2.function add(num1, num2{
console.log(num1, num2);
}
console.log(1,2) //return undefined
3.var name = undefined
console.log(name)// return undefined
4. var name;
console.log(name)// return undefined

double equal (==) vs triple equal (===)

Double equal(==) is convert to type to see if there is any type of value nearby then compare it to the same type and see if it is ok then return the result.

1. const strNum = '2'
if(strNum == 2) {
console.log('condition is true') // return 'condition is true'
}
else{
console.log('condition is true')
}

2.const strNum = 2
if(strNum == 2) {
console.log('condition is true') // return 'condition is true'
}
else{
console.log('condition is true')
}

triple equal (===) is check value and type bothe then return value true or false.

1.const strNum = 3
if(strNum === 3) {
console.log('condition is true') // return 'condition is true'
}
else{
console.log('condition is true')
}
2.const strNum = '3'
if(strNum == 3) {
console.log('condition is true')
}
else{
console.log('condition is true')// return 'condition is false'
}

Find the largest element of an array

const num = [12,3,23,45,66,32,77,12,3,5]
let max = num[0]
for(i = 0; i < num.length; i++){
let element = num[i];
if(element > max){
max = element
}
}
console.log(max)// return 77

remove duplicates item from array

let chars = ['A', 'B', 'A', 'C', 'B','D' , 'E' , 'B'];let uniqueChars = chars.filter((c, index) => {return chars.indexOf(c) === index});console.log(uniqueChars);

Reverse string

function reverseString(str) {// Step 1. Use the split() method to return a new arrayvar splitString = str.split("");// Step 2. Use the reverse() method to reverse the new created arrayvar reverseArray = splitString.reverse();// Step 3. Use the join() method to join all elements of the array into a stringvar joinArray = reverseArray.join("");console.log(joinArray)//Step 4. Return the reversed stringreturn joinArray; // "remmargorp olleH"}var res = reverseString("Hello programmer");console.log(res)// return remmargorp olleH

Dom

Dom meanings Document Object Model. Dom is a like tree data structure. Dom is a system where we can change HTML code via javascript. In this method, we can update, delete, read, etc html code via javascript.

bind, call and apply

The bind method is to bind a function to an external object. Then it will return a function. Next, we can call the function again.

function name(){
console.log('this name is' , this.name)
}
const obj1 = {
name: 'rakib',
age: 21
}
const result = name.bind(obj1)
result();//return "this name is rakib"

The call method becomes the call instantly and the function needs to pass as many arguments as it passes needs with a comma.

function number(a , b){
return (a * b) + this.num
}
const obj1 = {
num: 1,
age: 21
}
const result = number.call(obj1,4,6);
console.log(result)// return 25

The apply method same as call method just different is the arguments have to pass in the array.

function number(a , b){
return (a * b) + this.num
}
const obj1 = {
num: 1,
age: 21
}
const result = number.apply(obj1,[4,6]);
console.log(result)// return 25

Javascript Scope

javascript scope has two scopes one is local scope second is global scope

Local scope

When a variable is declared inside a function, it can be accessed within the area of that function and cannot be accessed outside it, it is called local scope. In this case.

Global scope

When a variable is declared outside the function, it can be accessed inside the function or anywhere, it is call Global scope.

--

--