
10 Advanced JavaScript Concepts
JavaScript is a programming language. It is very popular across the world. For beginners, there is much confusion in JavaScript. I was trying to clear up some confusion about JS string and JS math for beginners in the previous part. I hope I could help you to clear up your confusion. In this part, I am discussing 10 advanced JavaScript confusion. I hope you enjoy it….Let’s start
Null vs Undefined
When you set a value of a variable empty or null you will get the result ‘null’.But it didn’t set a value or set the value “undefined”(reserved word) you will get ‘undefined’.In a word, the result ‘undefined’ shows when you do not set a value.
Example-
let names = [“Rakib”, “Kabir”, “Faruk”, “Rahim”];
console.log(names[7]);
Ans: undefined
Double equal (==) vs Triple equal (===)
If you want to compare only the value of variables you can use double equal(==) but if you want to compare both the value and the type of variables you should use triple (===). Triple equal is known as “strict equality”.
Example-
With double equal
console.log(‘1’ == 1);
Ans: true
But with triple equal
console.log(‘1’ === 1);
Ans: false
map
If you want to use each element of an array, you should use the map to access them. The map makes work easier and keeps code cleaner.
Example-
I want to make square value of all elements of an array
const numbers = [1, 2, 3, 4, 5, 6, 7];
const result = numbers.map(x => x * x);
console.log(result);
Ans: [1, 4, 9, 16, 25, 36, 49]
filter
If you want to get any conditional elements with an array from another array, you should use the filter to get them.
Example-
I want to get the numbers those are less than 25 from this array
const numbers = [1, 4, 9, 16, 25, 36, 49];
const result = numbers.filter(x => x < 25);
console.log(result);
Ans: [1, 4, 9, 16]
find
If you want to get just a conditional element from an array, you should use the find to get that. Find returns just one value that meets the condition
Example-
I want to get the number which is greater than 25 from this array
const numbers = [1, 4, 9, 16, 25, 36, 49];
const result = numbers.find(x => x > 25);
console.log(result);
Ans: 36
scope (var, let, const)
If you want to declare any variable in your code you can use var, let, or const. There is some difference between those declarations. The difference is where you can access them. For declaring variable var is the most flexible. You can access the variable from anywhere in your code. If you want to access from the top of declaring your code didn’t crash just only shows the ‘undefined’ but you can not access the let and const from anywhere in your code. These variables have a block scope. You can only access this block.
Example-
console.log(today);
var today = “Friday”;
Ans: “undefined”(code will not crash)
console.log(today);
let/const today = “Friday”;
Ans: Code crushed.
slice
If you want to get any part of an array, you should use the slice to get them. The slice method works with the starting index to the end index. After getting your desired part from this array the main array remains the same.
Example-
I want to get the numbers which are starting from the index 2 to 6 from this array
const numbers = [1, 4, 9, 16, 25, 36, 49];
const result = numbers.slice(2, 6);
console.log(result);
Ans: [9, 16, 25, 36]
console.log(numbers);
Ans: [1, 4, 9, 16, 25, 36, 49] (The array remains unchanged)
splice
If you want to delete any part of an array and inject any other elements in this deleted position, you should use the splice to do that. The splice method works with a starting index and deleted count.
After deleting your desired part from an array the main array has become changed.
Example-
I want to delete the numbers which are start from index 2 and deleted count 2 from this array
const numbers = [1, 4, 9, 16, 25, 36, 49];
const result = numbers.splice(2, 2);
console.log(result);
Ans: [9, 16]
console.log(numbers);
Ans: [1, 4, 25, 36, 49] (The array has changed)
join
If you want to join the elements of an array, you should use join. You can join them without spacing, with whitespace, with commas, and many more.
Example-
I want to join the numbers of this array without any space
const numbers = [1, 4, 9, 16, 25, 36, 49];
const result = numbers.join(“”);
console.log(result);
Ans: 14916253649
break-continue
In your code, you can conditionally stop/skip your loop by adding break/continue. You can set a condition to stop your loop. When the loop went to the break condition it won’t run further, it stopped the loop immediately there.
But if you want to ignore a condition and run your loop, you can use the continue condition.
Example-
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for(let i = 0; i < numbers.length; i++) {
if(numbers[i] > 3) {
break;
}
console.log(numbers[i]);
}
Ans: 1, 2, 3 (Loop immediately stops after 3)
const numbers = [1, 2, -3, 4, 5, -6, 7, 8, -9];
for(let i = 0; i < numbers.length; i++) {
if(numbers[i] < 1) {
continue;
}
console.log(numbers[i]);
}
Ans: 1,2,4,5,7,8 (Loop skips all numbers less than 1 and finishes the loop)
Hope you enjoy the learning. That’s not the end of JavaScript. I will try to clear all the confusion step by step. Follow me and stay connected. Thank you…
Note: If you need a web developer to create your website, please visit Rakibul Hasan or https://www.fiverr.com/s/x5oKVZ