<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: habibur rahman</title>
    <description>The latest articles on DEV Community by habibur rahman (@habiburrahman).</description>
    <link>https://dev.to/habiburrahman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F627718%2F0096dba2-bac6-4646-84b6-f4a71dc243f1.jpg</url>
      <title>DEV Community: habibur rahman</title>
      <link>https://dev.to/habiburrahman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/habiburrahman"/>
    <language>en</language>
    <item>
      <title>Some Random Tricky Javascript Staffs</title>
      <dc:creator>habibur rahman</dc:creator>
      <pubDate>Sat, 08 May 2021 09:50:38 +0000</pubDate>
      <link>https://dev.to/habiburrahman/things-about-javascript-you-ll-have-fun-learning-3nl9</link>
      <guid>https://dev.to/habiburrahman/things-about-javascript-you-ll-have-fun-learning-3nl9</guid>
      <description>&lt;p&gt;1.&lt;strong&gt;Null vs Undefined&lt;/strong&gt;&lt;br&gt;
These are some ways you will get undefined..&lt;br&gt;
a)let name;&lt;br&gt;
console.log(name);//undefined&lt;/p&gt;

&lt;p&gt;b)if the function don't return&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(num1,num2){
console.log(num1+num2);
}
const res = add(5,6);
console.log(res);//11 undefined

const res2 = add(5);
console.log(res2);//undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c)Call an object's property that is missing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const movie = {name:'Social Network', director:'david Fincher',year:'2011'};
console.log(movie.cast);//undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;d)If you set a variable undefined&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = undefined;
console.log(a);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Null means empty. We can set a variable null. &lt;/p&gt;

&lt;p&gt;2.&lt;strong&gt;Double equal(==) vs Triple equal(===)&lt;/strong&gt;&lt;br&gt;
Double equal doesn't check the type of variable whether triple equal checks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const num1 = 5; //integer
const num2 = "5"; // String
if(num1==num2){
console.log("This is true");
}else{
console.log("This is false");
}
//Output: This is true.

if(num1===num2){
console.log("This is true");
}else{
console.log("This is false");
}
//Output: This is false.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.&lt;strong&gt;Closures vs Block Scope&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Scope:&lt;/strong&gt; Access something from inside of a function. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(num1,num2){
const res = num1+num2;
return res;
}
console.log(res);//error, because res is a variable inside the function, we can't access it from outside.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Global scope:&lt;/strong&gt;When a variable is declared globally, it can be accessed from outside of the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let total = 100;
function sub(num3){
return total+num3;
}
console.log(total);//here we can access the total variable from outside of the function.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Block Scope:&lt;/strong&gt;When you declare a variable inside 2nd brackets. They can't be accessed from outside of the area. &lt;br&gt;
{} =blocked scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(number &amp;gt;10){
let response = 'Happy';
console.log(response);
}

console.log(response); // error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; But if you use var to declare a variable. It will not show any error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(number &amp;gt;10){
var response = 'Happy';
console.log(response);
}
console.log(response); //no error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; It is called &lt;strong&gt;hoisting&lt;/strong&gt; in Javascript when a variable is declared using 'var' inside a scope'{}', it grabs the variable outside of the scope and presents it as a global variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Closures:&lt;/strong&gt; when a function returns another function. A closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function increase(){
let count = 0;
return function(){
count++;
return count;
}
}
const res1 = increase();
console.log(res1);//1
console.log(res1);//2
console.log(res1);//3

const res2 = increase();
console.log(res2);//1
console.log(res2);//2

console.log(res1);//4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.&lt;strong&gt;remove duplicate from an array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = [2,2,3,3,4,5,6];
const uniqueName = [];
for(let i =0;i&amp;lt;name.length;i++){
let element = name[i];
let index = uniqueName.indexOf(element);
if(index==-1){
uniqueName.push(element);
}
}
console.log(uniqueName);//[2,3,4,5,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.&lt;strong&gt;Reverse a String&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reverseString(str){
const reverse ='';
for (let i=0;i&amp;lt;str.length;i++){
let char = str[i];
reverse = char+reverse;
}
return reverse;
}
const sentence = 'Hello programming community';
const result = reverseString(sentence);
console.log(result);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6.&lt;strong&gt;Asynchronous&lt;/strong&gt;&lt;br&gt;
Javascript works synchronously. By saying that, I mean, javascript code finishes working one line after another. Example...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function movie(){
console.log('Ironman1');
}
console.log('Ironman2');
console.log('Ironman3');
//Output: 
Ironman1
Ironman2
Ironman3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But we can make javascript asynchronous. We can put a function or task on hold, by using the setTimeout() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function movie(){
console.log('Ironman1');
}
setTimeout(movie);
console.log('Ironman2');
console.log('Ironman3');
//Output: 
Ironman2
Ironman3
Ironman1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;We can also set a specific time for how long the function will be on hold. &lt;br&gt;
example,&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;setTimeout(movie,10000);//here time is in mili second.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;7.&lt;strong&gt;Event loop and Event Buble&lt;/strong&gt;&lt;br&gt;
8.&lt;strong&gt;let const and var&lt;/strong&gt;&lt;br&gt;
9.&lt;strong&gt;Recursive function&lt;/strong&gt;&lt;br&gt;
10.&lt;strong&gt;Call back function&lt;/strong&gt;&lt;br&gt;
11.&lt;strong&gt;CRUD Operation&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
