Facts you should know before starting js

Riyan Hasan
5 min readMay 6, 2021

Javascript is a dynamic language it has a lot of features it is not possible to learn every feature but one should learn some basics before starting it, to write good and efficient code.

Data Types in JS

Data is the most important factor in a program. We need to declare and initialize data whenever we start to write a program. Data types basically decide what data can we store or operate. There are six basic data types which again we can divide into three categories. Primitive, Composite, Special.

Primitive- String, Number, and Boolean

String Data Types — Use to represent textual data and its declared using double or single quotes.

let a = “I have started learning JS.”;

Number Data Types- The number data type is used to represent positive or negative numbers with or without decimal . 1.5e-4 (equivalent to 1.5x10–4,).There are different types of numbers integer, float, double etc

let a = 22344; // integer

let b = 8223.12; //floating

Boolean Data Types — Return only two values either true or false.

let isCoding = true; // yes, coding

let isSleeping = false; // no, not sleeping

Composite-

Object — Object is a complex data type that allows to the storage of a collection of data that is uniquely represented by a key.

const student = {

institute: “school”,

gender: “female”,

roll : 51

}

Array- An array is a type of data used for storing multiple values in a single variable. Each value has a numeric position, known as index started from 0.

var language = [“Python”, “Java”, “Ruby”];

Special-

Undefined- A variable has been declared but no value has been assigned.

let a;

Null-A null value means that there is no value. It is not equivalent to an empty string (“”) or 0, it is simply nothing.

var a = null;

Error Handling

Before starting a programming language we really need to learn and practice how to handle and manage errors. The try.catch and finally blocks are used to handle errors.

Types of Errors:

In programming, there can be two types of errors.

1.Syntax Errors — Syntax error is a simple error it might be a spelling error or any bracket

Or anything.

consol.log(‘your result’);

It is a syntax error, here should be console.log(‘your result);

2.Run time error — This type of error occurs during the execution of the program. These errors that occur during runtime are called exceptions. Now, let’s see how you can handle these exceptions.

The main code is inside the try block. While executing the try block, if any error occurs, it goes to the catch block. The catch block handles the errors as per the catch statements. If no error occurs, the code inside the try block is executed and the catch block is skipped.

Output

Comment in JavaScript-

Comments in programming are another Important basic. Comments in JavaScript are used to explain the code and make the program more readable for developers. In programming, comments can also be used to prevent some code lines from being executed. This is useful when testing. Single-line JavaScript comments are used for one line of comment only and do not need to be closed.

Multiline comments in JavaScript can comment out bigger parts (a few lines) of code and need to be closed.

Coding Style:

This is a very important matter for any professional programmer. Before learning to write in a spoken language, you must first learn the rules of grammar. Similarly, all programming languages must adhere to specific rules in order to function. This set of rules that determine the correct structure of programming languages is known as syntax. Many programming languages consist largely of similar concepts with variations in syntax.

Semicolon: While a sentence will end with a period, a JavaScript statement often ends in a semicolon (;).

Indentation: A complete JavaScript program can technically be written on a single line. However, this would quickly become very difficult to read and maintain. Instead, we use newlines and indentation.

Space: Whitespace in JavaScript consists of spaces, tabs, and newlines (pressing ENTER on the keyboard). As demonstrated earlier, excessive whitespace outside of a string and the spaces between operators and other symbols are ignored by JavaScript

Parentheses -For keywords such as if, switch, and for, spaces are usually added before and after the

parentheses. Observe the following examples of comparison and loops.

Scope

The scope is an important concept that manages the availability of variables.

There are two types of scopes: global scopes and local scopes.

1.Global scope- There can be one global scope in a code. A variable in the document usually declare

outside in the function.

2.Local Scope -Variables defined inside a function are in the local scope. And they have a different scope for every call of that function.

--

--