JavaScript: Some most basic things that developer needs to know
Hello everyone. My name is Asfakur Rahman. In this article i want to discuss about something basic bust most important things for all the JS developer. If you have some time, please read my article & follow me. Thank you
Here is a short list that i want to discuss in this article:
- Truthy and Falsy values
- double equal (==) vs triple equal (===), implicit conversion
- Scope, block scope, access outer scope
- Closure, encapsulation, private variable
- difference between call and apply
- Remove duplicate item from an array
- Count the number of words in a string
- Reverse a string
- Create a Fibonacci Series using a for loop
- Check whether a number is a Prime Number or not
Truthy & Falsy Values:
Truthy & Falsy are boolean type methods that need to know that the given value in the function or in the variable is true or false.
Here is a example of a Truthy & Falsy :
const age = 23;
if(age){
console.log("condition is true");
}
else{
console.log("condition is false");
}output: Condition is true;
In this example, if age variable value is 0 or less than 0, than the output would be false. Here are some values that define that output will be truthy or falsy:
Falsy values that gives false output:
0
""
undefined
null
NaN
falseTruthy values that gives true output:'0'
''
[]
{}
double equal (==) vs triple equal (===), implicit conversion:
double equal & triple equal are almost same but there is a some differences. double equal doesn’t check data type of a variable. That’s why when see that both variable are same then it will output that condition is true. Here is the example:
const number1 = 10;
const number2 = 10;
if(number1 == number2){
console.log("condition is true");
}
else{
console.log("condition is false");
}output: condition is true;
On the other hand, triple equal check data type of variable. if there is 2 value & one is a integer , one is string, then it output that the given condition is false. Example:
const number1 = 10;
const number2 = "10";
if(number1 === number2){
console.log("condition is true");
}
else{
console.log("condition is false");
}output: condition is false;
Scope, block scope, access outer scope:
Scope needs to declare inside the function. Rather than that it will won’t able to work perfectly. Example:
function total(first, second){
let result = first + second;
console.log(result);
return result;
}const output = total(5,10);
console.log(output);output: 15
15
If scope is outside of the function, those scope called as global scope. Here is the example of global scope:
let third = 20;
function total(first, second){
let result = first + second + third;
console.log(third);
return result;
}const output = total(5,10);
console.log(third);
console.log(output);output: 20
20
35
There is things that is called as “Hoisting”, that means a variable that can be declared after it has been used in function. In other words we can say that, a variable that can be used before it has been declared in function. Here is the example of block scope:
let bonus = 20;
function sum(first, second){
let result = first + second + bonus;
if(result > 9){
var mood = "happy";
mood = "fishy";
mood = "cranky";
}
console.log(mood);
return result;
}output: cranky;
Closure, encapsulation, private variable:
If you used to call one function from another function or you may return, then it makes a closed environment. After returning if it access the outside function, when it used all the variable with closed environment. Here is the example:
function stopwatch(){
let count = 0;
return function(){
count++;
return count;
}
}const clock1 = stopwatch();
console.log(clock1());
console.log(clock1());
console.log(clock1());
console.log(clock1());const clock2 = stopwatch();console.log(clock2());
console.log(clock2());
console.log(clock1());
console.log(clock2());output:1
2
3
4
1
2
5
3
Differences between call and apply:
call
Example:
const normalPerson = {
firstName: 'Rahim',
lastName: 'Uddin',
salary: 15000,
getFullName: function(){
console.log(this.firstName, this.lastName);
},
chargeBill: function(amount, tips, tax){
console.log(this);
this.salary = this.salary - amount - tips - tax;
return this.salary;
}
}const heroPerson = {
firstName: 'Hero',
lastName: 'Balam',
salary: 25000
}
const friendlyPerson = {
firstName: 'Hero',
lastName: 'Golam',
salary: 25000
}normalPerson.chargeBill.call(friendlyPerson, 5000, 500, 50);
console.log(friendlyPerson.salary);
console.log(normalPerson.salary);output: { firstName: 'Hero', lastName: 'Golam', salary: 25000 }
19450
15000
apply:
Example:
const normalPerson = {
firstName: 'Rahim',
lastName: 'Uddin',
salary: 15000,
getFullName: function(){
console.log(this.firstName, this.lastName);
},
chargeBill: function(amount, tips, tax){
console.log(this);
this.salary = this.salary - amount - tips - tax;
return this.salary;
}
}const heroPerson = {
firstName: 'Hero',
lastName: 'Balam',
salary: 25000
}
const friendlyPerson = {
firstName: 'Hero',
lastName: 'Golam',
salary: 25000
}
normalPerson.chargeBill.apply(heroPerson, [3000, 300, 30]);
normalPerson.chargeBill.apply(heroPerson, [4000, 400, 40]);
console.log(heroPerson.salary);output:{ firstName: 'Hero', lastName: 'Balam', salary: 25000 }
{ firstName: 'Hero', lastName: 'Balam', salary: 21670 }
17230
Remove duplicate item from an array:
Example:
var name = [3, 6, 2, 7, 3, 2, 8, 1, 9, 11, 56];
var uniqueName = [];for(var i = 0; i < name.length; i++){
var element = name[i];
var index = uniqueName.indexOf(element);
if(index == -1){
uniqueName.push(element);
}
}output: [3, 6, 2, 7, 8, 1, 9, 11, 56]
Count the number of words in a string:
Example:
var speech = "I am a good person. I don't snore at night";
var count = 0;for (var i = 0; i < speech.length; i++) {
var char = speech[i];
if (char == " ") {
count++;
}
}
console.log(count);output: 9
Reverse a string:
Example:
var statement = "Hello Alien bhai brother";
var forAlien = reverseString(statement);
console.log(forAlien);
var foodVlog = reverseString("Ki khawa jai... khida lagse");
console.log(foodVlog);output: rehtorb iahb neilA olleH
esgal adihk ...iaj awahk iK
Create a Fibonacci Series using a for loop:
Example:
var fibo = [0, 1];for(var i = 2; i <= 10; i++){
fibo[i] = fibo[i-1] + fibo [i-2];
}
console.log(fibo);output: [
0, 0, 1, 2, 3, 5,
8, 13, 21, 34, 55, 89,
144
]
Check whether a number is a Prime Number or not:
Example:
function isPrime(n){
for(i = 2; i < n; i++){
if(n % i == 0){
return 'Not a prime number';
}
}
return 'Your number is a prime number';
}
var result = isPrime(139);
console.log(result);output: Your number is a prime number
Thanks for reading.