Javascript Random Topics

Photo by Goran Ivos on Unsplash

topics I covered in this lesson:

  1. values in js
  2. expression
  3. using typeof
  4. try…catch block
  5. commenting
  6. coding styles guide
  7. best practice to write a function
  8. what is the best for indentation: tab or space?
  9. using ESLint
  10. some good resources for js styling guides

1. values in js:

There are two types of values in javascript. They are:

  • primitive types — when this type of value is put in a variable then the ‘value’ is saved in that variable.
  • reference types — when this type of value is put in a variable then the value of the type is not saved rather its reference(where the data is in the memory) is saved.

primitive types:

  1. undefined
  2. null
  3. string
  4. number
  5. boolean
  6. symbol

reference types:

  1. object — (that means function, array are also reference types)

2. what expression is:

Simply expression is a sentence that produces value.

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

In the above code, both the sentences are expressions. Because they are producing value.

3. using typeof:

We know that there are many types of data in javascript. Now the question is, is there any tool to inspect the data type? The answer is “yes ”and the name of that tool is typeof. So let’s use it:

typeof({}) // object 
typeof('abc') // string

another way to use it:

typeof 2 //number 
typeof 'abc' // string

so we can use typeof with parenthesis or without parenthesis. But there is a problem with using typeof without parenthesis. Let’s look at such a situation:

typeof n=>2*n //will produce an error

So using parenthesis is better with typeof.

4. try catch block:

Error occurs in programming. We can’t stop it. If an error occurs, program execution stops immediately and your app will crash. So it’s better to find out a way so that the program progress as well as gives an error message so that we can fix the bug easily. For this task, we have try…catch block.

syntax:

try{
//statements
}
catch(err){
//error message
}

how it works:

if the statements in the try block are correct, then the program executes them and ignores the catch portion. But we have to make sure that everything is syntactically correct.

5. commenting:

commenting is very important in programming. It helps to easily understand the code and the functionality. But more comment is not good. Rather we need to balance. If we can organize our code in a proper way then more commenting not necessary. By the way, we can do single-line and multi-line commenting.

example:

// single line comment/*multi
line
comment
*/

6. coding styles guide:

If we can organize our code in a proper way, then bug fixing becomes easy and readability increases. It is one of the most important skills for a good programmer. In the subsequent lessons, I will cover some good practices in javascript programming.

7. Good practice to write a function:

Let’s look at an example:

function myFunc(n, m){
//statement 1;
//statement 2;
return something;
}
  1. Look at the function name, it’s camel-case(joining two words making the second word’s first letter capitalized)
  2. no space should be given between parenthesis and function name.
  3. look at the parameters separated by a comma. After every comma put a space and write the parameter.
  4. putting semicolon is a good practice. Although it is not needed in js.

8. tab or space for indentation?

Developers recommend using 2 spaces for indentation rather than using a tab. Because spaces provide more flexible configuration.

9. using ESLint:

Linters are tools that automatically check our code and make it better using a given configuration and they also provide some bug fixing suggestions. There are many linters available. Of them, Eslint is the most popular. Eslint is built-in vs code. It’s better to use that and our worries about coding style reduces when we use these tools.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tanvir Ibn Touhid

I am very passionate about web development, programming. I am very much skilled in javascript, react, html, css, node js, express, mongodb etc.