JavaScript tricky things.

Mehedi hasan
3 min readNov 5, 2020

1.truthy or falsy :

a. any number without 0 is true number. 0 is a falsy number

let age = 18;if(age){console.log(`condition is true`) // expected output condition is true} else {console.log(`condition is false`)}

b. any kinds of string without empty string is truthy.

let greetings = ‘’;if(greetings){console.log(‘condition is true’);} else {console.log(‘condition is false’); // expected output condition is false}

c. unassigned variable is falsy value.

let greetings;if(greetings){console.log(‘condition is true’);} else {console.log(‘condition is false’); //expected output condition is false}

d. variable assigned on null and NaN value is a falsy value.

let greetings = null;if(greetings){console.log(‘condition is true’);} else {console.log(‘condition is false’); //expected output condition is false}let greetings = NaN;if(greetings){console.log(‘condition is true’);} else {console.log(‘condition is false’); //expected output condition is false}

2.undefined:

a. any variable which is declared but value is not assigned is a undefined value.

let greetings;console.log(greetings); // expected output undefined

b. A function which explicitly not return a value is a undefined value.

function add (x, y) {console.log(x + y);}let result = add(3, 2);console.log(result); // undefined

c. any parametre inside function which is not passed value is undefined value.

function add (x, y) {console.log(x + y);}let result = add(3);console.log(result); // undefined

d. property doesn’t exist in an object it becomes undefined.

const person = {name: ‘mehedi hasan’,age: 20}console.log(person.education); // undefined

3. null:

null represent the intentional absence of any object values.

4. == vs === equal:

double equal compare only value but triple equal compare not only value but also data type. In triple equal, if data type and value remain same then it’s becomes true whether it’s becomes false.

const first = 5;const second = ‘5’;if(first === second) {console.log(‘condition is true’);} else {console.log(‘condition is false’); // expected output condition is false}

5. bind:

bind method create a new function, has this keyword to set provided value.

const nPerson = {name: ‘mehedi hasan’,age: 34,salary: 30000,chargeBill: function(amount) {return this.salary = this.salary — amount;}}const hPerson = {name: ‘araf mahmud’,age: 22,salary: 20000,}const charge = nPerson.chargeBill.bind(hPerson);const chargingAmount = charge(2000);console.log(chargingAmount); // expected output 18000

6.call:

call methods call a function with a given this value and arguments provided individually. argument declared with comma.

const nPerson = {name: ‘mehedi hasan’,age: 34,salary: 30000,chargeBill: function(amount) {return this.salary = this.salary — amount;}}const hPerson = {name: ‘araf mahmud’,age: 22,salary: 20000,}nPerson.chargeBill.call(hPerson, 2000);console.log(hPerson.salary); // expected output 18000

7.apply:

apply method calls a function with a given this value and arguments provided as an array. difference between apply and call is call declared variable with comma but apply declared argument as an array.

const nPerson = {name: ‘mehedi hasan’,age: 34,salary: 30000,chargeBill: function(amount, tax) {return this.salary = this.salary — amount — tax;}}const hPerson = {name: ‘araf mahmud’,age: 22,salary: 20000,}nPerson.chargeBill.apply(hPerson, [2000, 1000]);console.log(hPerson.salary); // expected output 17000

8.global variable:

In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the global environment or global state.

9. this keyword:

‘this’ keyword used to follow the current existent in an method.

const person = {firstName: ‘John’,lastName: ‘Doe’,getFullName: function () {this.fullName = this.firstName + ‘ ‘ + this.lastName;return this.fullName;}}console.log(person.getFullName());

--

--