10 javascript tricky things you should know.

Nk Rafi
4 min readMay 17, 2021

--

  1. Null and Undefined:

Null: when you want to declare a variable, but don’t want to assign any value to that variable, then you can assign a value call null to that variable. Null is a primitive value that represents the intentional absence of any object value. The type of null value is an object.

let myVar = null;
console.log(myVar);
console.log(typeof(myVar))
#output:
null
object

Undefined: when a variable is declared but no value is assigned the variable returns a value called undefined. The type of undefined is the string “undefined”.

let myVar;
console.log(myVar);
console.log(typeof(myVar));
#output:
undefined,
"undefined"

2. Truthy and Falsy values:

There is a data type in javascript called Boolean. Boolean contains two values true and false. Using these Boolean values you can check other data types Truthiness or falseness. For better understanding, an example is given below.

function truthyOrFalsy(myVar) {      
if(myVar) {
return true
} else {
return false
}
}
let result = truthyOrFalsy(3);
let result2 = truthyOrFalsy('');
#output:
true
false

There are 6 falsy values in javascript, which are 0, null, undefined, false, NaN, and “” (empty string). Except for these 6 values all the other values return true. Values like an array, object, string, number.

3. Double equal and Triple equal:

Double equal (==) in JavaScript is used for comparing two variables, but it ignores the data type of the variable. Where triple equal(===) is used for comparing two variables, but this operator also checks data type and compares two values. It returns true only if both values and data types are the same for the two variables.

let myVar = 5;
console.log(myVar == '5')
console.log(myVar === '5')
#output:
true
false

4. Variable Scope:

Scope refers to the accessibility of a variable in javascript. Javascript has two types of scope golobal scope and local scope (known as block or function scope). When you decliare a variable outside of any function or condition the variable will be available in anywhere in the code, but when a variable decliared inside any function or condition that variable is available only inside that function or condition.

let myVar = "Hello"
function myFunction() {
let myVar2 = "World";
let greethings = myVar + " " + myVar2;
return greethings
}
let result = myFunction()
console.log(result)
let result2 = myVar + " " + myVar2
console.log(result2)
#output:"Hello World"VM638:9 Uncaught ReferenceError: myVar2 is not defined
at <anonymous>:9:29

5. Closure:

Closure means that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned. Inner function can access variables and parameters of an outer function (however, cannot access arguments object of outer function). For better understanding an example given below:

function OuterFunction() {

var outerVariable = 100;

function InnerFunction() {
console.log(outerVariable);
}

return InnerFunction;
}
var innerFunc = OuterFunction();
innerFunc();
#output:
100

6. The this keyword in javascript:

The this references the object of which the function is a property. In other words, the this references the object that is currently calling the function. Suppose that you have an object called counter. This object counter has a method called next(). When you call the next() method, you can access the this object.

let counter = {     
count: 0,
next: function () {
return ++this.count;
}
};
counter.next();
#output:
1

7. The call() method in javascript:

The method Call invokes the function and allows you to pass in arguments one by one using commas. Example given below:

let person1 = { name: 'Mike', email: 'Mike@gmail.com' };
let person2 = { name: 'liam', email: 'liam@hotmail.com' };

function greeting(greetingText) {
console.log(`${greetingText} ${this.name}`);
}

greeting.call(person1, 'Hello');
greeting.call(person2, 'Hello');
#output:
"Hello Mike"
"Hello liam"

8. The apply() method in javascript:

The method Apply invokes the function and allows you to pass in arguments as an array. Example given below:

let person1 = { name: 'Mike', email: 'Mike@gmail.com' };
let person2 = { name: 'liam', email: 'liam@hotmail.com' };
function greeting(greetingText, greetingText2) {
console.log(`${greetingText} ${this.name}, ${greetingText2}`);
}
greeting.apply(person1, ['Hello', 'How are you?']);
greeting.apply(person2, ['Hello', 'How are you?']);
#output:
"Hello Mike, How are you?"
"Hello liam, How are you?"

9. The bind() method in javascript:

The Bind method returns a new function, allowing you to pass in a this array and any number of arguments. Use it when you want that function to later be called with a certain context like events. Example given below:

let person1 = { name: 'Mike', email: 'Mike@gmail.com' };
let person2 = { name: 'liam', email: 'liam@hotmail.com' };
function greeting(greetingText) {
console.log(`${greetingText} ${this.name}`);
}
let greetingMike = greeting.bind(person1);
let greetingLiam = greeting.bind(person2);
greetingMike('Hello');
greetingLiam('Hello');
#output:
"Hello Mike"
"Hello liam"

10. What is an API in javascript?

An API (Application Programming Interface) is a set of functions that allows applications to access data and interact with external software components, operating systems, or microservices. To simplify, an API delivers a user response to a system and sends the system’s response back to a user. Using a API call you can Create, Read, Upadate and Delete data from a database which is konwn as CRUD operation. Fatch method is most commonly used for calling and performing an opertaion using an API. In Fetch API post method is used for posting data, get method is used for geting data, and patch method is used for updating and delet method is used for deleting data from a database.

--

--

Nk Rafi
Nk Rafi

Written by Nk Rafi

0 Followers

Tech writer. Bridging complex tech and plain language. Passionate about innovation. Educating and inspiring readers to stay ahead.

No responses yet