10 Things in JavaScript

adnan sarkar06
5 min readMay 6, 2021

--

1. Primitive Values

In JavaScript, there are many primitive values like “String”, “Number”, “Null”, “undefined”, “Boolean” etc. Which are not manipulated by us like any Object or Function.

At first, you may think what is difference between number & object!!! because call and store value what we need in there. But wait, there is something special for you 😉

primitive values examples

look at this example, there are 2 variables x, y. I assign x into y and console.log() you see there is nothing special, After assigning y in a new value that is the same thing before we get.

reference values

OMG! why x values get changed? Yes, That is the main point, primitive values are just what we use, not manipulate like objects. In Object, they pass their reference not their values that’s why addOne function changes the main reference where store same object x.

2. Typeof()

typeof() is a method to use to know the data type. Sometimes you may need to know what type of data type in the value. Yes it’s pretty helpful for conditions, like “if the value is string doing this”. for example,

it’s helpful when we doubt the values data type.

3. Coding Style

Let’s talk about coding style. It’s helpful for any reader/other developers to understand your code fast. Then you have to write codes following standard. some examples below:

standard coding style

You need to write two parameters using space to separate them & it looks clear far away from the screen. The indentation between 2 lines, the empty line between two separate functionality codes. So many code editors support some extensions for code formatting like “prettier”, which helps us to write code efficiently and clear. You can avoid style and your code runs well.

4. Comments

Every programming code has a comment system. It’s the main thing to understand the functionality summary in one/more lines of comments. So the comment is a line of summary text which is not affecting our main code. That is not run in our compiler.

comment in JavaScript

For single-line comment you need to start “//” in a line, then that line will consider commenting.

single-line comment

Sometimes single-line didn't meet demand, then comes multiple-lines commenting. Explaining what you do you do in the front line of codes.

multi-lines comment

5. Arrow Function

Arrow functions come in the es6 version in JavaScript. It’s easy to call a function & some efficient features. It can call one line only 😎, multiple lines what you need. But the main thing is Arrow function has no name 😨 so you need to call it like function expression.

arrow function

In the example, there are 2 types to call arrow function. First, you need to pass the parameter then you need to write the “=>” arrow symbol then you write your functionality & last you need to return it. Second, if you write it one line you don’t need to return, but if you use {} then you need to write return.

6. Spread Operator

It’s come same as the arrow function in es6. In the modern JavaScript framework, there is a need to include all array elements in the new array. In this case, you need to Loop through the array to push all elements into the new array. So, this problem fixed our spread operator. Its syntax looks like […oldArray].

spread operator in JavaScript

7. Block Scope

If/else or any Loops where you write functionality inside {}, then it’s to be considered a block scope. let & const keyword is fully dependent in a block scope, its attitude into a block scope one thing and outside another thing.

you can’t access any block element outside(global) the block, but you can access anything into the block scope from outside(global).

Look you get an error to access data in global scope, which is inside block scope.

8. Default Parameter

The default parameter is set as a value for backup 😷. sometimes in the function, you set two-parameter but when calling the function you forgot to set a second value and get an error. so, if you set any default parameter in the second parameter then its help you to fulfill when you don’t set value in function calling.

default parameter

When you set the default parameter and set your value in function calling, then the default parameter doesn’t work, think it’s like a backup 🤐.

9. map()

map is one of the common method you need to use. It works for array and alternative for loops. When use map in the array it will return the same copy of the main array. map iterate all elements of the array one by one, so you can do something like loops when it iterates one by one.

map in JavaScript

map returns all square root in numbers elements.

10. Destructuring

Destructuring is useful for long object/array to access data to new variables. When you need to access data from an object using dot notation it’s so boring thing. So you can use restructuring in your code, for example:

destructuring array & object

Look a,b are new variables and their values are arrays index 0 &1 values, same as an object but the object has no index, so, if restructure variable name is same as object property it will get that property value.

--

--

adnan sarkar06
adnan sarkar06

No responses yet