Javascript Must Know Topics

Here we will discuss few must know topics of javascript.

Primitive Value or Primitive Data Type

In Javascript, a primitive (primitive value, primitive data type) is a data that is not object and has no methods. All primitives are immutable (cannot be altered).

There are 6 primitive data types: string, number, bigint, boolean, undefined and symbol. null also is a special primitive type.

  • undefined - used for unintentionally missing values.
  • null - used for intentionally missing values.
  • boolean​ (true and false) - used for logical operations.
  • number - used for math calculations.
  • string​ ("hello", 'abracadabra') - used for text.
  • Symbol (uncommon) - used to hide implementation details.
  • BigInt (uncommon and new) - used for math on big numbers.
// Using a string method doesn't mutate the string
const str = "foyez"
str.toUpperCase() // "FOYEZ"
console.log(str) // "foyez"

In Javascript, Everything that is not a primitive type is an object.

Function Arity

In Javascript, the number of arguments a function takes is called function arity. It is possible to pass more arguments than the defined parameters. We access to the all argument using arguments operator.

function add(a, b = 1) {
  console.log("arguments: ", arguments.length) // 4

  const sum = a + b
  console.log("sum: ", sum) // 3

  const argumentsSum = [...arguments].reduce((acc, val) => acc + val, 0)
  console.log("argumentsSum: ", argumentsSum) // 10
}

add(1, 2, 3, 4)

typeof

The typeof operator returns the type of the operand in a string.

It can be written in two ways.

typeof operand
typeof(operand)

Example:

typeof 10 // "number"
typeof "foyez" // "string"
typeof true // "boolean"
typeof undeclaredVar // "undefined"
typeof [] // "object"

Error handling using "try...catch"

There are thousand of reasons, like typo, an unexpected user input, an erroneous server response, to occur errors in our code.

Usually, a script dies (immediately stops) in case of an error and printing it to console.

Here try...catch comes to save our codes from dying. try...catch allows us to catch errors.

It won't work if the code is not valid, like syntactically wrong.

It works synchronously.

try {
  // code
} catch (err) {
  // error handling
}

It works like this:

  1. First, the code in try{...} is executed.
  2. If there were no errors, then catch(err) is ignored.
  3. If an error occurs, then the try execution is stopped, and control flows to the beginning of catch(err).

Comments

In javascript, comments can be single-line: string with // and multiple: /* ... */

Comments are essential to describe how and why the code works.

There's a great rule about comments: “if the code is so unclear that it requires a comment, then maybe it should be rewritten instead”.

Good comments

  1. describe the architecture
  2. Document function parameters and usage
  3. Why is the task solved this way?
  4. Comment as much as needed but not more.

Example 1:

/* use escapes for non-printable characters with a comment for clarity. */
return "\ufeff" + content // Prepend a byte order mark.

Example 2:

/**
 * Returns x raised to the n-th power.
 *
 * @param {number} x The number to raise.
 * @param {number} n The power, must be a natural number.
 * @return {number} x raised to the n-th power.
 */
function pow(x, n) {
  ...
}

Truthy & Falsy values

In JavaScript, there have 7 falsy values — they all evaluate to false in conditionals. Without this 7 falsy values, all are truthy values.

  • Falsy: 0, BigInt On, null, undefined, false, NaN, "" (equivalent to '' or )
  • Truthy: [], {}, 4, etc.