Javascript- Concepts you need to know

Abu Said Md. Rezoun
3 min readMay 7, 2021

In this blog, we will talk about some important concepts that you should know if you want to be a javascript developer.

Data types: You can check the type of a variable by using typeof() method. Let’s say we have a variable x=5; so, typeof(x) will return ‘number’. There are a few other types of values that are available in javascript. Let’s have a look-

● Undefined​ (undefined), used for unintentionally missing values.
● Null​ (null), used for intentionally missing values.
● Booleans​ (true and false), used for logical operations.
● Numbers​ (5, -10, 7.2, and others), used for math calculations.
● Strings​ (“book”, “flower”, and others), used for text.
● Symbols​ (uncommon), used to hide implementation details.
● BigInts​ (uncommon and new), used for math on big numbers.

Primitive values: Let’s do an experiment. Open a browser like chrome or firefox. Go to the console panel and you are going to type the following lines

console.log(2);
console.log({});
console.log([]);

If you did it correctly the output is going to be like this

Now, look at the second and third output has an arrow sign but the first one doesn’t. It is a way of representing primitive values. Number and strings are primitive values and the next two are objects.

typeof(): It can be used to check the type of a value or variable. For example-

console.log(typeof(2)); // "number"
console.log(typeof("hello")); // "string"
console.log(typeof(undefined)); // "undefined"
console.log(typeof({})); // "object"
console.log(typeof([])); // "object"
console.log(typeof(x => x * 2)); // "function"

try.. catch: The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed if an error occurs in the try block.

try {   
// Try to run this code
} catch(err) {
// if any error, Code throws the error
}

try..catch..finally: The try/catch/finally statement handles some or all of the errors that may occur in a block of code, The catch and finally statements are both optional, but you need to use one of them (if not both) while using the try statement.

try {   
// Try to run this code
}
catch(err) {

// if any error, Code throws the error
} finally {
// Always run this code regardless of error or not
//this block is optional
}

Spread Operator: The spread operator is a new addition to the set of operators in JavaScript ES6. It takes in an iterable (e.g an array) and expands it into individual elements. The spread operator is commonly used to make shallow copies of JS objects. Using this operator makes the code concise and enhances its readability.

function sum(x, y, z) { 
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers));
// expected output: 6
// This will add each item in number arrray in sum method.

Arrow Function: Arrow function is one of the features introduced in the ES6 version of JavaScript. It allows you to create functions in a cleaner way compared to regular functions.

// Non Arrow (standard way)
let add = function(x,y) { return x + y; }
console.log(add(10,20)); // 30
// Arrow style
let add = (x,y) => x + y;
console.log(add(10,20)); // 30;

Default parameter in Javascript: The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined. In this case, the default value that we specify is applied by the compiler.

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

Variable Hoisting: Hoisting in JavaScript is a behavior in which a function or a variable can be used before declaration. For example,

x = 10;
console.log(x);
var x; // 10

In terms of variables and constants, keyword var is hoisted and let andconst does not allow hoisting. In hoisting, though it seems that the declaration has moved up in the program, the actual thing that happens is that the function and variable declarations are added to memory during the compile phase.

In this blog, we tried to learn about some core concepts of javascript. This will help you learning javascript clearly.

--

--

Abu Said Md. Rezoun
0 Followers

I'm a CSE graduate who loves Javascript (MERN stack) developing. I'm a ML enthusisat and I like to do problem solving.