Some JavaScript Concepts for Beginner part-2

Md Saddam Hossain Knight
2 min readMay 6, 2021

01. Var Declarations:

The var keyword declares a function or globally-scoped variable.

Ex.
var a = 10;
if (a === 10) {var a = 20;console.log(a); // output: 20}console.log(a); // output: 20

02. Let Declarations:

The let keyword declares a block-scoped local variable. The variable type let shares a lot of similarities with var but, unlike var, has scope constraints. Its declaration and assignment are similar to var. When a programmer needs to variable scope, let keyword helps them.

Ex.
let b = 10;
if (b === 10) {let b = 20;console.log(b); // output: 20}console.log(b); // output: 10

03. Constant Declarations:

Constants (const keyword) are block-scoped, much like the let keyword. The value of a const keyword can’t be changed through reassignment, and it can’t be redeclared.

Ex.
const num = 420;
try {num = 9999;} catch (error) {console.log(error); //output: Error: ‘Assignment to constant variable.’// Note — error messages will vary depending on browser}console.log(num); // output: 420

04. Default function parameters:

Default function parameters allow named parameters to be initialized with default values if no value or undefined is provided.

Ex.
function multiply(a, b = 10) {
return a * b;}console.log(multiply(9, 8)); //output: 72console.log(multiply(43)); // output: 430

05. JavaScript Anonymous Functions:

An anonymous function is a function without a name. This function is often not accessible after its initial creation.

Ex.
let showMessage = function () {
console.log(‘This is Anonymous function’);};showMessage(); //output: This is Anonymous function

06. Spread Operator:

ES6 introduced a new operator referred to as a spread operator, which consists of three dots (…). It allows an iterable to expand in places where more than zero arguments are expected. It gives us the privilege to obtain the parameters from an array.

Ex.
let numbers = [10, 20];
let newNumbers = […numbers, 30,40, 50];console.log(newNumbers); // output: [10, 20, 30, 40, 50 ]

07. Arrow function:

Arrow functions were introduced in ES6.

Arrow functions allow us to write shorter function syntax.

Ex.
Normal Function:
text= function() {return “Happy Coding!”;}Arrow Function:text = () => “Happy Coding!”;

08. JavaScript Single-line Comment:

Single line comments start with //.

Ex.
//Normal Function
text= function() {return “Happy Coding!”;}

09. JavaScript Multi-line Comments:

Multi-line comments start with /* and end with */.

Ex.
/*
This isthe exampleof normal function*/text= function() {return “Happy Coding!”;}

10. try…catch:

The try…catch keyword marks a block of statements to try and specifies a response should an error be thrown.

Ex.
const num = 420;
try {num = 9999;} catch (error) {console.log(error);// expected output: Error: ‘Assignment to constant variable.’// Note — error messages will vary depending on browser}console.log(num);// expected output: 420

--

--