Some Things to Know about JavaScript

1. Default parameter in JavaScript functions.

Default parameter allows named parameters to be initialized with default values which is very useful how you say code example is given.

With Default parameter
function multiply(a, b = 1) {
return a * b;
}

console.log(multiply(3, 2)); // expected output: 10
console.log(multiply(3)); // expected output: 5

Without Default parameter
function multiply2(x, y) {
return x * y;
}
console.log(multiply2(4, 3)); // expected output: 12
console.log(multiply2(3)); // expected output: NaN

in the first code, everything is working fine but in the second code, NaN was generated because the second parameter was not passed. Es6 Default parameter in functions is a great addition.

2. What is This Spread syntax (…)

In Es6 Spread operator is a great addition for function, array and object. in a function for passing multiple parameter values in a single go and copying concatenating arrays and objects has become easier because of spread syntax. The code examples are given.

// Function Example
const numbers = [1, 2, 3];
function sum(a, b, c) {
return a + b + c;
}
console.log(sum(…numbers)); // expected output: 6

// Array Example
let arrMain = [1, 2, 3];
let arrClone = […arrMain]; // like arr.slice()

arrClone.push(4);
console.log(arrClone) // arrClone becomes [1, 2, 3, 4]
console.log(arrMain) // arrMain remains unaffected

let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
let arr3 = […arr1, …arr2];

console.log(arr3) // concatenated in arr3

// Object Example
let obj1 = { foo: ‘bar’, x: 42 };
let obj2 = { foo: ‘baz’, y: 13 };

let copyObj = { …obj1 };
console.log(copyObj) // Object { foo: “bar”, x: 42 }

let joinedObj = { …obj1, …obj2 };
console.log(joinedObj) // Object { foo: “baz”, x: 42, y: 13 }

3. Es6 Arrow Function.

An arrow function is a compact alternative to a traditional function. but it can not be used in all situations. It doesn’t have its bindings to this or super and should not be used as a method. not suitable for a call, apply and bind methods. it generally relies on establishing a scope. code example shown-

const materials = [
‘jamil’,
‘Abdullah’,
‘cline’,
‘bob’
];

console.log(materials.map(material => material.length));
// expected output: Array [8, 6, 7, 9]

4. Var Declarations and Hoisting.

Es6 adds a new feature that is called hoisting. in other word hoisting is a mechanism that handles execution contexts in JavaScript. In JavaScript, a variable can be declared after it has been used.

x = 5;
console.log(5) // Assign 5 to x
var x; // Declare x

This means that we can access the x variable from everywhere and in the enclosing block in case of var binding. To address this issue with var ES6 introduces block-level declarations using let and const.

5. Block-Level Declarations in JavaScript.

Ok, let us say that you have declared a variable within a block-level scope using of let and const you cannot access the declaration from the outside of that block scope. Block scops can be created from inside a function block or logical if block wrapped in curly braces.

6. Block Binding in Loops in JavaScript.

the variable i is still accessible after the loop is completed because the var declaration gets hoisted. Using let instead, as in the following code, should give the intended behaviour the variable i only exists within the for loop. Once the loop is complete, the variable is destroyed and is no longer accessible elsewhere.

// Exapmle with var
for (var i=0; i < 10; i++) {
console.log(i) // 0–9
}

console.log(i); // i is still accessible here 10

// Example with let
for (let i=0; i < 10; i++) {
console.log(i) // 0–9
}

console.log(i); // i is not accessible here

7. List of Primitive Types in JavaScript.

Strings = used for text.
Numbers = used fom math Calculations.
Undefined = used for unintentionally missing values.
Null = used for intentionally missing values.
Booleans = logical operation true of false.
Symbol = used to hide implementation details.
BigInts = used for math on big numbers.

8. What is Event loop in JavaScript.

A very interesting property of the event loop model is that JavaScript, unlike a lot of other languages, never blocks. Handling I/O is typically performed via events and callbacks.

The call stack is responsible for keeping track of all the operations in line to be executed. Whenever a function is finished, it is popped from the stack.

Whenever an async function is called, it is sent to a browser API. These are APIs built into the browser.

The event queue is responsible for sending new functions to the track for processing. It follows the queue data structure to maintain the correct sequence

The event loop facilitates this process; it constantly checks whether or not the call stack is empty. If it is empty, new functions are added from the event queue. If it is not, then the current function call is processed.

9. Error handiling in JavaScript.

No matter how great you are at programming, sometimes your scripts have errors. they may occur because of your mistake an unexpected user input, an erroneous server response and any other reasons. so for error handling, the try-catch syntax is used if an error occurs the catch block will execute. some times an unexpected data error might occur but the catch block does not get the error so implicitly throw an error in the try block.

10. Comments in JavaScript.

Comments are very important in JavaScript. that are two types of comment single-line comment starting with // and multiline comment starting and ending with /**/.
but if a programmer code properly there is no need for comments proper code shows their presence and even their absence. Good comments allow us to maintain the code well.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

How to Create a JavaScript Bar Chart

Do you Promise?

can we change to (collectors)

JavaScript ES6: A Major Upgrade

How to Display 360 Images Using React and Select Coordinates

Javascript Algorithms

Reactime Version 12 Updates

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
Shaikh Jamil AL-Razi

Shaikh Jamil AL-Razi

More from Medium

Encore! XWIN comes back to the field at BabySwap

The Speed To Market of Cardano vs. Solana, Ethereum, and Bitcoin

​​What is BoozCoin?

Hello Everyone..!