JavaScript tricks you should know!

Towhidul S Islam
4 min readMay 6, 2021

1.Primitive Value

A primitive value is a data, it is not an object and not has method. There are seven primitive data such as string, number, bigint, undefined, Boolean, symbol and null. Primitive values are like stars -cold and distant, but always there when I need them.

Example: console.log(2); Output: 2

console.log(“hello”); Output: hello

console.log(undefined); Output: undefined

2. Object and Function

Objects and functions are important because ​we can manipulate them from our

code​. For example, we can connect them to other values. This is rather vague —

so we’ll refine this idea in a later module. For now, we can say that if primitive

values are like distant stars, then objects and functions are more like rocks

floating nearby my code. They’re close enough that I can manipulate them.

Example: console.log({}); Output: {}

console.log([]); Output: []

console.log(x => x * 2); Output: f()

3. Expressions

Some problem JavaScript will not be answer. You can not solve this problem with JavaScript. But some problem JavaScript has proper answer and this problem has special name that are called expressions. It can provide us a single value. If we question JavaScript 2+2 and JavaScript answered you 4.

Example: console.log(2+2); Output: 4

4. Type of

We can find the type of a value. It is very important for us. We use typeOf to know the type of value. We can find many type of value such as Boolean, number, string, undefined, null and more. So we can find the taype of value by using typeOf.

Example: console.log(typeof({})); Output “object”

console.log(typeof([])); Output”object”

console.log(typeof(x => x * 2)); Output: “function”

Coding Style

5. Curley braces

Curley braces is most important and essential thing in coding. I t is very useful. We use curley braces as the Egyptian style. We should use it at opening braces on the same line as the corresponding keywords.

Example: if (condition) {

// do this

// …and that

// …and that

}

6. Line Length

No one likes to read a long horizontal line of code. It’s best practice to split them.

// backtick quotes ` allow to split the string into multiple lines

let str = `

ECMA International’s TC39 is a group of JavaScript developers,

implementers, academics, and more, collaborating with the community

to maintain and evolve the definition of JavaScript.

`;

And, for if statements:

if (

id === 123 &&

moonPhase === ‘Waning Gibbous’ &&

zodiacSign === ‘Libra’

) {

letTheSorceryBegin();

}

7. Indents

A horizontal indentation is made using either 2 or 4 spaces or the horizontal tab symbol (key Tab). Which one to choose is an old holy war. Spaces are more common nowadays. It is good for use tab button to implement the 2–4 spaces.

One advantage of spaces over tabs is that spaces allow more flexible configurations of indents than the tab symbol.

For instance, we can align the parameters with the opening bracket, like this:

Example: show(parameters,

aligned, // 5 spaces padding at the left

one,

after,

another

) {

// …

}

function pow(x, n) {

let result = 1;

// ←

for (let i = 0; i < n; i++) {

result *= x;

}

// ←

return result;

}

8. Semicolons

A semicolon is need to after if statement. It very much important. There are languages where a semicolon is truly optional and it is rarely used. In JavaScript, though, there are cases where a line break is not interpreted as a semicolon, leaving the code vulnerable to errors. See more about that in the chapter code structure.

If you’re an experienced JavaScript programmer, you may choose a no-semicolon code style like standaerdJS. Otherwise, it’s best to use semicolons to avoid possible pitfalls. The majority of developers put semicolons.

9. Comments

Comments are very much important. If we find a code in thousand lines of code, it is very much hard to find the proper one. So, we should comment the code. We normally describe how work the code by commenting in the code. If we see our code after a few years then we can understand code easily by read the comments. And every programmer should implement comments in the code. At first sight, commenting might be obvious, but novices in programming often use them wrongly.

10. Browser testing

Cross browser testing is the practice of making sure that the web sites and web apps you create work across an acceptable number of web browsers. As a web developer, it is your responsibility to make sure that not only do your projects work, but they work for all your users, no matter what browser, device, or additional assistive tools they are using. You need to think about:

  • Different browsers other than the one or two that you use regularly on your devices, including slightly older browsers that some people might still be using, which don’t support all the latest, shiniest CSS and JavaScript features.
  • Different devices with different capabilities, from the latest greatest tablets and smartphones, through smart TVs, right down to cheap tablets and even older feature phones that may run browsers with limited capabilities.
  • People with disabilities, who use the Web with the aid of assistive technologies like screenreaders, or don’t use a mouse (some people use only the keyboard).

--

--

Towhidul S Islam
0 Followers

Hi! I am a professional Web Developer. I am working with JavaScrpt, React, Node , Express.js and MongoDB.