Beginner JavaScript

imam hasan
4 min readMay 6, 2021

1. JavaScript Types of Values:

There are actually only nine types of values in JavaScript. These nine types are divided into two groups.

Primitive Values:

  • Numbers
  • Strings
  • Booleans
  • Null
  • Undefined
  • BigInts
  • Symbols

Objects and Functions:

  • Objects
  • Functions

2. Comments In JavaScript:

Comments are a very important thing in the code. We use the comment to describe the code simple so that we can understand our own logic of the code in the future or if other developers see my code then they can also understand that.

In JavaScript, we can add a single line comment starting with // and multiline comment /*. . . */

3. Try . . . catch:

In our daily life of coding, we usually make mistakes as we are human beings. In JavaScript, the script stops immediately after finding any error. But the try … catch works for find any error and instead of stoping the script, it could do a more reasonable thing like show an alert about the error.

Syntax:

The code execution steps are following:

  • first, the code inside the try block tries to execute.
  • if there is no error, then the execution reaches the end of the try block and skips the catch block.
  • if there occurs any error in the try block, then the rest of the code in the try block skipped, and control flows to the beginning of the catch (err) block. The err variable contains an object about the details of the error occurs try block.

4. Throw operator:

The throw operator generates a custom error.

Syntax: throw <error object>

We can use primitives as error objects, but it’s better to use an object with a name and error message.

JavaScript has many built-in constructors to make the standard error like Error, SyntaxError, ReferenceError, TypeError, and others.

Syntax:

Now we can make our own custom error by following:

5. Try . . . catch . . . finally:

Now the try … catch construct has another part that is called finally. What happens to the try … catch block, finally always executes.

Syntax:

6. Functions:

Functions are one of the fundamental parts of JavaScript. Generally, it takes input as an argument and returns the output as a result of the calculation and there is some obvious relation between input and output.

Function Declarations:

Syntax: function functionName(parameter){ function-body }

the previous function receives a number as a parameter and returns a cube of the parameter.

Function Expression:

A function can also be created with function expression

Syntax: const functionName = function(parameter) { code }

the previous function works exactly as the function declaration.

7. Arrow Function:

Arrow function is a simple version of the function expression.

Syntax: const funcName = (arg1, arg2, . .) => expression

lets look at an example:

8. Differences & Limitations of Arrow Function:

Though the arrow function is a simple and very easy version of function expression, it can not be used in all situations. It has some differences and limitations.

  • does not have its own bindings to this or super
  • should not be used as methods.
  • does not have arguments or new.target keywords.
  • not suitable for call, apply and bind method, which generally relies on establishing a scope.
  • can not be used as a constructor.
  • can not use yield, within its body.

9. The Spread Syntax:

Spread Syntax is used when all elements from an object or array need to be included in a list.

In the above example, we pass an array of 3 numbers to the add function by spreading them so that they can be passed to the parameter separately.

10. Hoisting:

Hoisting was thought up for a general way of thinking about how execution contexts work in JavaScript. In a way, the definition of hoisting suggested that variables and function physically moved to the top of your code, but this never happens. Instead, the variables and the functions are stored in the memory during the compile phase, but stay exactly where you typed your code.

In the above example, we can use the function before the declaration of that function because of hoisting.

--

--