Skip to content

Latest commit

 

History

History
114 lines (71 loc) · 4.11 KB

js-basics.md

File metadata and controls

114 lines (71 loc) · 4.11 KB

← Back

JavaScript Basics

Table of Contents


Strict Mode

To active strict mode, include the following string at the beginning of the script:

"strict mode";

Strict mode helps developers avoid accidental errors, identify bugs, write secure code.

  • Strict mode forbids us to do certain things
  • It will create visible errors for us in certain situations
  • Introduces a short list of variable names that are reserved for features that might be added to the language latter

Equality Operators

Two equality operators: == and ===

Differential operators: !== and !=

  • The strict equality operator === does not perform type coercion. It only returns true when both values are exactly the same.
  • The loose equality operator == does type coercion, "18" == 18; // true

General rule: always use the strict equality operator ===


Operator Precedence

JavaScript has a well defined order of operator precedence (order in which operators are executed).

Table of Precedence

The grouping () parenthesis has the highest priority in precedence, always are executed first.

In JS, assignment operators work from right-to-left - let x = 10 + 2; - first is calculated the value from the right, and then the value is assigned to the variable.


Statements and Expressions

The difference between expressions and statements is important to know, because JavaScript expects statemenets and expressions in different places. For example, in a template literal, we can only insert expressions, but not statemenets.

  • Expressions are small units of code that produce a value. They can be thought of as combinations of values, variables, operators, and function calls that result in a single value.

    Expressions can be used within statements to calculate values, assign values to variables, or perform various operations.

    Examples of expressions include mathematical calculations, function calls, and variable references.

    let y = 10; // Assignment expression
    let sum = 5 + 3; // Arithmetic expression
    let greeting = "Hello, " + "world!"; // Concatenation expression
    let result = Math.max(5, 8); // Function call expression
  • Statements are complete instructions that perform actions. They don't necessarily produce a value but instead perform an operation. Statements often end with a semicolon ;

    Examples of statements include variable declarations, control structures like if and for loops, function declarations, and assignments.

    let x; // Variable declaration statement
    if (x > 5) {
      // Conditional statement
      console.log("x is greater than 5"); // Function call statement
    }

String Interpolation

We can insert (interpolate) variables into strings using template literals (ES6 Feature).

const myName = "John";
let me = `I'm ${myName}`;
console.log(me); // I'm John
  • A template literal is wrapped by backticks ``
  • Variables are inserted inside curly parentheses ${} syntax
  • We can insert any js expression in the template literal placeholder

Another great use of template literals is to create multi-line strings. Very useful when we build HTML from JS. Before ES6, multi-line strings was more harder to write using /n/.


The DRY Principle

DRY - don't repeat yourself.

Refactoring - restructuring and improving our code without changing how it works (e.g. eliminating duplicate code).

For example, when we have the same pieces of code in multiple places, we can refactor the same code into a function, and then call that function in all the places where we have the duplicate code. We refactor functionality that we use over and over again into their own function in order to make the code more DRY.