10 Fundamental Concepts of JavaScript

Rifa Tasfia
4 min readNov 3, 2020

Function

A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when something calls it.

Example:

A function for adding two numbers

Output:

Output

Here, we can see that when we call add function and pass value 28 & 36 it returns a output containing the summation of the two numbers.

Function Scope

Variable defined inside a function can only be accessed in the scope of the function. It can not be accessed from outside. A function can access all variables and functions defined inside the scope in which it is defined. This is called function scope.

But a variable defined globally can be accessed from anywhere.

Default Parameter

In JavaScript, a parameter has a default value of undefined. If we initialize a function with parameter, but when we call the function we don’t pass a value, then the function will return NaN.

Example:

A function for adding two numbers

Output:

Output

If we want to solve the problem we can set a default value.

Example:

A function for adding two numbers with default parameter

Output:

Output

Spread Operator (…)

… It is a magical feature of JavaScript. It allows to spread out elements of an iterable object such as an array, a map, or a set. It is often used to copy an array or an object.

Example:

Implementation of spread operator

Output:

Output

Block Level Function

A block scope is the area within if, switch conditions or for, while and do-while loops. whenever { } appears, it is a block. Const and let are block level variables, which means those variables exist only within the corresponding block.

Arrow Function

An arrow function is an alternative to traditional function. The whole thing can be done by typing a little with arrow function

Example:

With traditional function

With traditional function

Output:

Output

With arrow function

With arrow function

Output:

Output

Var Declarations and Hoisting

In JavaScript, the default nature is to move all declaration to the top. This is called hoisting.

If a variable is declared using var, then it will be treated as it is at the top of the function. Meaning that using var keyword, we can first initilize a value and then declare the variable. Using var keyword declarations are initialized with undefined. But using let or const keyword declarations remain uninitialized.

Comment

A comment is a programmer-readable explanation. They are used to make to code easier to understand.

There are two types of commenting style.

  1. Single-line comment (//) : When we want to comment a single line or part of a line we use single-line comment.
  2. Multi-line comment (/* ……. */) : When we want to comment multi line we use multi-line comment.

Coding Style

While coding one must try to make the code as clean as possible. For this, if one follows some coding style, then it will be easier for anyone to understand the code.

Some suggested rules for writing code are listed below:

  1. A space between parameters
  2. Spaces around operators
  3. Curly brace { on the same line, after a space
  4. An empty line between logical blocks
  5. Spaces around a nested call
  6. A semicolon ; is mandatory
  7. A space before and after for/if/while/do-while
  8. No space between the function name and parentheses between the parentheses and the parameter

Try…catch

When executing JavaScript code, different errors can occur. Errors can be coding errors made by the programmer, errors due to wrong input, and other unforeseeable things. To handle error we can use try…catch method.

The try statement lets you test a block of code for errors.

The catch statement lets you handle the error.

Syntax:

try {

//code

}

catch{

// error to handle

}

--

--

Rifa Tasfia
0 Followers

I am an undergraduate student of IIT, Jahangirnagar University. I am very much passionate about learning new technologies.