Skip to main content

Basics of JavaScript

Variables and Values

A variable is a holder for a representation of a value.

Value is the smallest piece of information that is stored in memory. There are two types of values in JS. They are either objects or primitive values

Data types of JavaScript

There are 7 primitve data types in JavaScript.They are:

  1. Number: Floating point numbers. Used for decimals and integers
  2. String: Sequence of characters. Used for text. Can be wrapped in ' ' or " "
  3. Boolean: Logical type that can only be true or false
  4. Undefined: Value of a variable that is not defined. eg: let steve; this will hold undefined
  5. Null : Empty value
  6. BigInt: Large numbers that cannot be held by Number
  7. Symbol: Immutable and unique

Variable Declaration

There are 3 types of variable declaration in Javascript.

  • var: allows mutating. Avoid using
  • let: allows mutating.
  • const: use when the value is to not be mutated.
    • Initial value is required.
    • Good practice to use this as first option.

If there is no keyword before declaring a variable then the variable will be declared in the global scope.

Code Snippets for the same

let name;
// will return undefined
let age = 22;
// used let as age changes
const birthYear = 2001;
// used const as birthYear doesnt change

// Global declaration
openSource = 'This project is open source';
console.log(openSource); //output => This project is open source

Operators

There are many categories of operators:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Conditional Operators
  6. Type Operators

Their precendence can be found on the table present in Mozilla Developer Network (MDN)

Arithmetic Operators

These operators are used for arthimetic (mathematic) operations.

OperatorDescription
+Addition
-Subtaction
*Multiplication
**Exponent (Raised to the power)
/Division
%Modulo (Division Remainder)
++Increment
--Decrement

Assignment Operators

These operators are used to assign a values. They follow right hand assignment i.e the value on the right is assigned to the left. Below are short hands of it as well

OperatorExampleSame as
=x = yx = y
+=x += yx = x + y
-=x -= yx = x - y
*=x *= yx = x *y
/=x /= yx= x/y
%=x%yx= x%y
**=x **=yx= x ** y

+ can also be used to concatenate strings

let text = 'What a wonderful ';
text += 'day it is';

console.log(text); // output => What a wonderful day it is

Comparison Operators

OperatorDescription
==Equal to
===Equal value and Equal type
!=Not equal
!==Not Equal value or not equal type
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
?Ternary operator (checks boolean)

Logical Operators

OperatorDescription
&&logical AND
||logical OR
!logical NOT
  • They can be use any data type, return any data type.

  • Often used for short-circuiting.

    Short-circuiting for || (OR)
    console.log(3 || 'Steve'); // output =>3
    console.log('' || 'Steve'); //output => Steve
    // Evaluates the first expression and returns it if true and ignores the second expression.
    //Else returns second.
    Short-circuiting for && (AND)
    console.log(0 && 'Steve'); //output =>0
    console.log(7 && 'Steve'); //output => Steve
    // Evaluates till first false value is found.
    // Else continues till expression is true.
  • The OR operator returns false when compared with 0 or ' '.

    • The Nullish Coalesing Operator overcomes this pitfall.
    • It treats only null and undefined as false.
    const restaurantGuests = 0;
    const guests = restaurantGuests ?? 10;
    console.log(guests); // output=>0
  • Logical operators can be combined with assignment operators as well to assign values.

Type Operators

OperatorDescription
typeofReturns the type of variable
instanceofReturns true if an object is an instance of object type