A must-know for all JavaScript learners

Samia Farid
5 min readNov 3, 2020
  1. Primitive values

In javascript, primitives values are:-

  • String: (“hello”, “flower”), used as text.
  • Number: (45. 653), used as numbers in any calculation.
  • Boolean: (true or false), used as logical operations.
  • Undefined
  • Null
  • BigInt
  • Symbol

They are called primitive values because they are unchangeable once declared in a variable, but they can be reassigned to a new value and not change.

Unlike, objects and functions, they are not primitive values as they can be changed.

2. Error handling, “try..catch”

This try..catch in javascript allows us to handle runtime errors.

It has two main blocks, which is try and then catch

  • The code in try{..} block will be executed and if it works properly then the catch block will be ignored.
  • If there is an error in try block, it will jump to catch block to handle the error and not break or stop the code there.
  • Then, the catch block will provide the name of the error and message of the error

If you want to handle an error for a function, try.catch should be inside the function

Example: try..catch syntax

3. Coding Style

One’s code must clean and easy to read as much as possible.

  • There shouldn’t be any gap between the function’s name with the parentheses
  • After for or while loop inside the function, start by giving 2 space indentation and space.
  • A backtic can be used to split the string in multiple lines and make it readable.
  • It’s better to use semicolons when a statement ends in javascript to avoid any future possible accidents while coding.
  • Automated linters are tools that can automatically check the style of your code and improving suggestions.

4. Comments

Which programmer doesn’t know how to comment, but there is something known as bad and good comment.

Bad comment

  • Bad comment is when someone describes ‘how this code works’ or ‘what does this code do’
  • You shouldn't comment for a function, as the function itself says what it does .
  • Explanatory comment should be avoided and keep the comment minimal
  • No need of giving comment to a place where it does not require

Good comment

  • A good comment is when a person tries to avoid giving comments as much as possible to a necessary place.
  • Avoiding commenting to describe a function, they should make a function self-descriptive.
  • They can use some special syntax called JSDoc as well to interpret javaScript.

5. Block-Level Declarations

Block-level declarations in javascript are those that declare variables that are inaccessible outside of the given block scope. Block scopes are created:

  • Inside a function
  • Inside of a block made by curly brackets

Let declarations

  • The let declaration syntax is the same as the syntax for var. Basically, var can be replaced with let to declare a variable, but let will limit the variable’s scope to only the current code block and will be available to the entire block.

Constant declarations

  • In javascript, some variables are declared with const, which is constant meaning once declared, their values cannot be changed. Hence, they are initialized on declaration only.

6. Block Binding in Loops

  • Like the above example, the variable is declared by var, hence, the variable ‘i’ is still accessible after the loop is completed because the var declaration gets hoisted (moved at the top and becomes a global variable)
  • To prevent this, let should be used to declare a variable. Therefore, the variable ‘i’ only exists within the for loop now. Once the loop is complete, the variable is destroyed and is no longer accessible elsewhere.

7. Global Block Bindings

Let, const and var are different in their behavior in the global scope.

  • When var is declared in the global scope, it creates a new global variable, which is a property on the global object (windows in browser).
  • That means, you can accidentally overwrite an existing property in global object using var.
original global property is overwritten
new property added in the global object
  • to prevent this, usage of let and const will only create a new binding in the global scope, but no property will be added in the global object or anything being overwritten.

8. Var Declarations and Hoisting

Variable declared using var are treated as if they are at the top of the function (or global scope, if declared outside of a function) regardless of where the actual declaration occurs; this is called hoisting.

9. Balancing client and server caching

Client caching

  • Client caches help limit the data cost sustained by the user by keeping commonly referenced data locally. The client often requests data that may not be large but is indeed continuously needed.
  • From the API point of view, the client makes a request to the API. The client first looks locally for the relevant data. If this data isn’t found, the request is then sent to the external resource, and the content is generated for the requesting client.
  • The advantage here is that the network for the client does not face heavy traffic demands as many content requests can be kept local. Additionally, this frees up time on the server-side, which no longer has to field repeat queries that have already been answered.

Server caching

  • Server caching helps limit the cost sustained by the server and its underlying systems. Many requests made by clients can either be responded to using the same data or responded to using parts of the same requests made by others.
  • well, this doesn’t save much cost for the client, but the savings to the server can be quite significant.

10. Arrow Function

Arrow function works the same as normal function, just a different and better way to denote it.

  • arrow function with 1 parameter
  • arrow function with 2 parameters, if more then separate it with a coma
  • arrow function with no parameter
  • multi-line arrow function with return

--

--