How to improve your JavaScript skills

1. Truthy And Falsy

The following values are always truthy:

· “0” (a string containing zero, it’s always truthy)

· “false” (a string containing false, it’s truthy)

· { } (empty object always truthy)

· [ ] (empty array always truthy)

· Function{ } (an empty function)

The following values are always falsy:

· False

· 0 (zero)

· ‘ ‘ (empty)

· Null

· Undefined

· NaN

2. What is a Null and Undefined?

Null and undefined both are data types of JavaScript

Null: as an assignment value that means it’s blank.

Undefined: is a variable that has been declared but it’s not assigned a value.

3. Difference between triple (===) equal vs double (==) equal.

The double equals operator compares the valus and returens the true or false.

Example:

Console.log( null == undefined) / return true

Console.log( null == NaN ) /return false

The triple equal operator not only check the values but alsp check the type of variable. If the values are not the same type it returns flase.

Example:

Console.log( 2 === 2 ) / return true

Console.log( 2 === ‘2’ ) /return false

4. JavaScript Scope

There are two type of scope

1. Function scope: when you declare variable in the function, the result get if you call function name. You can’t access the variable outside the variable.

Example:

function foo(){

var fruit =’apple’;

console.log(‘inside function: ‘,fruit);

}

foo(); //inside function: apple

console.log(fruit); //error: fruit is not defined

2. Block function: a block scope function is the area within if, switch condition. Whenever you see curly brackers{}, it is a block. In ES6, let and const are allow to declare varable in the block scope but var not allow in block scope.

Example:

function foo(){

if(true){

var fruit1 = ‘apple’; //exist in function scope

const fruit2 = ‘banana’; //exist in block scope

let fruit3 = ‘strawberry’; //exist in block scope

}

console.log(fruit1);

console.log(fruit2);

console.log(fruit3);

}

foo();

//result:

//apple

//error: fruit2 is not defined

//error: fruit3 is not defined

5. Difference between call(), apply() and bind()

Call( ) : the call() method function with a given ‘this’ value and arguments provided one by one.

For example:

Var friend1 = {firstName: ‘Boni’, lastName: ‘islam’}

Var friend2 = {firstName: ‘Hafiz’, lastName: ‘ali’}

Function invite(greeting1, greeting2){

Console.log(greenting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName + greeting2)

}

Invite.call(friend1, ‘hey’ ‘what’s up?’)

Result = hey boni islam, what’s up?

Invite.call(friend2, ‘hey’ ‘what’s up?’)

Result = hey hafiz, what’s up?

Apply() : apply function allow to pass the arguments in an array.

For example:

Var friend1 = {firstName: ‘Boni’, lastName: ‘islam’}

Var friend2 = {firstName: ‘Hafiz’, lastName: ‘ali’}

Function invite(greeting1, greeting2){

Console.log(greenting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName + greeting2)

}

Invite.call(friend1, [ ‘hey’ ‘what’s up?’ ] )

Result = hey boni islam, what’s up?

Invite.call(friend2, [ ‘hey’ ‘what’s up?’] )

Result = hey hafiz, what’s up?

Bind() : return a new function, allowing you to pass in array and any number of arguments

For example:

Var friend1 = {firstName: ‘Boni’, lastName: ‘islam’}

Var friend2 = {firstName: ‘Hafiz’, lastName: ‘ali’}

Function invite(greeting1, greeting2){

Console.log(greenting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName + greeting2)

}

Var inviteFrined1 = invite.bind(friend1)

Var inviteFrined2 = invite.bind(friend2)

inviteFrined1 (‘hey’ ‘what’s up?’)

Result = hey boni islam, what’s up?

nviteFrined2 (‘hey’ ‘what’s up?’)

Result = hey hafiz, what’s up?

6. Global scope and global variable

Global scope refers to all variable declare in js file that are not inside any function. These variable are access anywhere in the js file.

For example:

var carName = “Volvo”;

// code here can use carName

function myFunction() {

// code here can also use carName

}

7. What is ‘this’ keyword in JavaScript?

‘this’ keyword refers to an object that object which is executing the current bit of JavaScript code.

Example:

var obj1 = { name: “Pulsar”, bike: bike };
var obj2 = { name: “Gixxer”, bike: bike };

function bike() {
console.log(this.name);
}

8. JavaScript timing events

Javascript can be executein time-interval, this is called timing events.

There are two method of timing events:

1.setTimeOut

The syntax:

let timerId = setTimeout(function, [delay])

function sayHi() {

alert(‘Hello’);

}

setTimeout(sayHi, 1000);

2.setInterVal

The syntax:

let timerId = setInterval(function, [delay])

9. Find the largest element in array

var array = [3 , 6, 2, 56, 32, 5, 89, 32];

var largest= 0;

for (i=0; i<=largest;i++){

if (array>largest) {

var largest=array[i];

}

}

console.log(largest);

10. JavaScript Arrow function

Arrow function is using in lieu of function(). Arrow function is a ES6 related function.

For example arrow function:

const friends = [

{

Name: “Boni”,

Profession: “Marketer”,

Age: 22

},

{

Name: “Hafiz”,

Profession: “Web Developer”,

Age: 23

},

{

Name: “Monir”,

Profession: “Banker”,

Age: 21

}

]

const friendsLength = friends.map(friend => friend.length)

console.log(friendsLength)

Output result = 3

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store