Javascript 10 valuable method you must be needed.

prince shafayat
4 min readNov 7, 2020

1. Objects

We know about javascript variable. Variable use for store data value. Object same as a variable but object store many values. Like “laptop” is an object. Laptop has property like “ram”, “ hdd”, “motherboard” etc. All laptop have the same property but property values are different.

var laptop = {name : "asus", hdd: "500gb", ram: "4gb"}
console.log(laptop);
// expected output: Object { name: "asus", hdd: "500gb", ram: "4gb" }
console.log(laptop.name);
// expected output: "asus"

2. function

Any programming language you learn you get has to function. All language function concepts almost the same. The function gives a name on a codeblock and then anywhere call back by the name. We can call a function much time with different arguments, return a different result.

see following example :

function welcomeMessage(name){
return 'Congratulations '+ name+' you are now our new member.'
}
console.log(welcomeMessage(Shafayat))
// expected output: "'Congratulations Shafayat you are now our new member "
console.log(welcomeMessage(Abdullah))
// expected output: "'Congratulations Abdullah you are now our new member "

3. typeof()

The typeof operator uses when we need to know the data type return as a string. The typeof use in two ways.

syntax

typof operand

or

typof(operand)

Javascript have almost 9 type of value.

• Undefined
• Null
• Booleans
• Numbers
• Strings
• Symbols
• BigInts
• Objects
• Functions

see the following example:

console.log(typeof(105));     // "number"
console.log(typeof("shafayat")); // "string"
console.log(typeof([])); // "object"
console.log(typeof({})); // "object"
console.log(typeof($)); // "undefined"
console.log(typeof(true)); // "boolean"
console.log(typeof(()=>{return 2+2})); // "function"

4. try..catch

The code executed in try{…}. Skip the catch() if have no error in the try{}. But catch() execute handle the error and try{…} execution stopped if try{…} have any error.

Here is a chart :

try-catch

see the following example :

try{
alerrrrt("Hi I'm Shafayat")
}catch(error){
console.log(error);
}
// expected output: ReferenceError: alerrrrt is not defined

5. Var Declarations and Hoisting

var used for variable declaration. A variable declared top of the current script or the function. In other words, the variable declared after used, this is called hoisting.

see the following example :

var a = 100;function checkHoisted() {

a = 50;
var b = 70;

}
hoisted()console.log(a);
// expected output: 50
console.log(b);
// expected output: Error: b is not defined

in the example script, “ a ” is a hoisted variable, “ b ” is a non hoisted variable.

6. Block-Level Declarations

The variable can not use outside of a specified block or function that is call block-level declarations. ES6 release new block-level declaration identifier let or const.

see the following example

function getMySurprise(vl) {  
if (vl==true) {
let surprise = "New iPhone 10";
// other code
return surprise ;
} else {
// value doesn't exist here
return null;
}
// value doesn't exist here
}
//=> Just for test
console.log( getMySurprise (true) );
// expected output: New iPhone 10

Error: surprise is not defined will be shown If we try to use the function’s inside “surprise” variable to use outside.

console.log(surprise );
// expected output: Error: surprise is not defined

7. Block Binding in Loops

Block Binding in Loops means the declared variable only can use in the loop, not accessible, or update outside of the loop. So what variable we need to use, we need to use let or const. Not use the var variable because var is a global variable.

See the following example (is not block binding in the loop);

for(var t=0; t < 10; t++ ){
// do something
}
console.log(t);
// expected output: 10
Now we use letfor(let t=0; t < 10; t++ ){
// do something
}
console.log(t);
// expected output: Error: t is not defined

8. Global Block Bindings

The var variable is different from let & const. The var is a global variable when we use it outside of a function, but the var is accessible by any function. We can update the building javascript global variable because it’s global.

See the example : Here is the javascript building global variable

console.log(location);
// expected output: https://shafayat18.medium.com/javascript-10-valuable-method-you-must-be-needed-5ea834eb3aeb

see the example: Now we change the global variable.

var location = "http://google.com"
console.log(location);
// expected output: "http://google.com"

we can change the “location” variable because it’s a global variable.

9. Functions with Default Parameter Values

We can use in function one or more parameter. We can declare a default parameter value if no value or undefined is passed. It avoids getting an error.

See the following example :

function welcomeMessage (name="user") {
const message = 'Thank you ' + name + ' for visit our site.';
return message;
}
console.log(welcomeMessage());
// expected output: "Thank you user for visit our site."
console.log(welcomeMessage("Shafayat"));
// expected output: "Thank you Shafayat for visit our site."

10. The Spread Operator

The spread operator gets out of the value from the iterable object where uses one or more value. The spread operator call by 3 dots( … ) before the iterable object. In the ES6 version the spread operator added.

see the following example:

var laptop = ['dell','asus','samsung'];console.log(laptop); // without spread result
// expected output:Array ["dell", "asus", "samsung"]
console.log(...laptop); // with spread result
// expected output: "dell" "asus" "samsung"
// expected output: Object { name: "asus", hdd: "500gb", ram: "4gb" }console.log(laptop.name);// expected output: "asus"

--

--