Some Special Topics Of JavaScript
--
null vs undefined
- null is an assignment value by the user
Example:
let st = null; // st is declared & assinged
if (st === null ) {
console.log(“null is found”);
}
- undefined is not an assignment value but declared by the user
Example:
let st; // st is declared but not assinged
if (st === undefined ) {
console.log(“undefined is found available”);
}
Double Equal(==) vs Triple Equal(===)
- “==” & “===” are value-comparison operations in JavaScript. They work same but additional property is contained in “===” checks to compare equality the data-type of two variables.
Example:
var num = 0;
var obj = new String('0');
var str = '0';console.log(num == num); // true
console.log(obj == obj); // true
console.log(str == str); // true
console.log(num === num); // true
console.log(obj === obj); // true
console.log(str === str); // true
console.log(num == obj); // true
console.log(num === obj)// false
console.log(obj === str); // false
console.log(null === undefined); // false
console.log(obj === null); // false
console.log(obj === undefined); // false
Implicit Conversion
JavaScript helps to convert a wrong value to right value.
console.log(2+ “4”); // output: 24 which is a String
console.log(2++ “4”); //output: 6 which is a Number
Three Array Functions In JavaScript ES6
map
- map returns an array with the same array length
Example:
let array = [3, “m”, 4, 5.4];
array.map( (item, idx) => console.log(item[idx])); // Output: 3 , “m”, 4, 5.4 which are in the five different line.
filter
- filter returns an array which is less or equal to the orginal array.
Example:
Example:
let array = [3, “m”, 4, 5.4];
const newArray = array.filter((item, idx) => item[idx] !== 3);
console.log(nwArray); //Output: [“m”, 4, 5.4]
find
- find returns a first value from the original value from an array
Example:
const array = [3, “m”, 4, 5.4];
const flag = 4;
const findItem = array.find((item, idx) => item[idx] === flag );
console.log(findItem); // Output: findItem = 4
reduce
- reduce returns a single value (or object).
Example:
const array1 = [1, 2, 3, 4];
const reducer = (acc, cur) => acc + cur;// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
The scope of variables in JavaScript
Scope is the region of the codebase over which an identifier is valid. A lexical environment is a mapping between identifier names and the values associated with them.
The four scopes are:
- Global — visible by everything
- Function — visible within a function (and its sub-functions and blocks)
- Block — visible within a block (and its sub-blocks)
- Module — visible within a module
var
Identifiers declared using var have function scope
let & const
Identifiers declared using let & const have block scope
new
The new keyword is used to create an user-defined object type or the built-in object types that has a constructor of the class.
Example:
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}const car1 = new Car(‘Eagle’, ‘Talon TSi’, 1993);
console.log(car1.make);
// expected output: “Eagle”
this
this is a pointer of the object in a block scope. In most cases, the value of this is determined by how a function is called (runtime binding).
Example:
const test = {
prop: 42,
func: function() {
return this.prop;
},
};console.log(test.func());
// expected output: 42