JS Basic

Data Types

primitive - basic data type, value of which has no properties and methods

  • all data types except object are primitives
  • all primitives are immutable

there are 8 data types in JavaScript

boolean - represents only two values: true & false

  • falsey values - those values which are being converted into false. i.e. "", 0, NaN, null, undefined


number - represents numeric values. i.e. integer, float, NaN, Infinity

string - just a set of characters

object - represents various data structures. core objects: Object, Array, Function, RegExp, Date, Error

  • technically, everything that can be invoked with "new" keyword results in type object value
  • note: typeof functionInstance === 'function' because function is a special type of object, called "callable object"


bigint - represents very big numbers

  • e.g. 553366447n


symbol - represents primitive value which is guaranteed to be unique

null - represents intentional absence of value. it indicates that absence is temporal and might change to something else later

  • nullish values - null, undefined
  • note: typeof null === 'object' due to historical context. i.e. previously null was treated as a value, which represents the absence of an object


undefined - represents the absence of value

JS Basic

Operators

operators - symbols that performs operations on variable values or literals

Arithmetic

+, -, *, / - basic ones

% - remainder of two numbers

** - exponentiation

Comparison

=== - strict equality
!== - string inequality

== - non-strict equality
!= - non-strict inequality

  • compared to strict ones, these do convert data type
  • e.g. 4 == '4' - returns true, because string is being converted to number


>, <, >=, <= - greater than, less than, greater than or equal, less than or equal

Logical

&& - AND
|| - OR
! - NOT

  • !! - convert to boolean


?? - nullish coalescing operator. returns a non-nulllish value

Assignment

= - assignment
+=, -=, *=, /=, %= - addition assignment, subtraction assignment, etc.

++ - increment

  • x++ - return and increment
  • ++x - increment and return

-- - decrement

Logical Assignment

&&= - logical AND assigment
||= - logical OR assignment

  • e.g. x ||= y - operator assigns only when "x" is falsey
  • old analog: fooVariable = fooVariable || 'some value'


??= - nullish coalescing assignment

Other

typeof - data type check

  • returns one out of 8 possible strings: "undefined", "boolean", "number", "string", "bigint", "symbol", "object", "function"
  • all primitive data types except null can be correctly identified with typeof operator


instanceof - checks whether an object inherits methods from specific constructor function or class

  • syntax: object instanceof Constructor
  • e.g.
  • new Foo() instaceof Foo - returns true
  • {} instanceof Object - returns true. constructor might be further in prototype chain


void - calculates and returns undefined

^ - bitwise XOR

Iteration

for (initialization; condition; finalExpression) {...} - syntax of for loop

  • when using "let" for initialization, a fresh copy of variable will be created for each iteration, as variable declarations are block-scoped
  • fresh copies of variable are useful when working with asynchronous code

for, while, do while are primary loops in JS. they work similar like in other programming languages, except JS specifics such as hoisting and closures

for (const key in object) {...} - walk over keys of an object

  • loop iterates over inherited properties too
  • Object.keys & Object.values can also be used for object iteration. see Objects -> Static Methods for more


for (const value of iterable) {...} - walk over values of an iterable object

  • continue & break - keywords to manage loops
  • tricks:
  • for (let [key, value] of Object.entries(object)) {...} - iterate over keys and values of simple object


iterable object - an object that can be iterated over with the loop construct like "for...of" or "forEach"

  • e.g. Array, TypedArray, String, NodeList, arguments, Map & Set, etc.
  • iterating methods like map, filter, etc. are exclusive to Array instances

if else

if (condition) {...} - allows executing a statement conditionally

  • condition - code that evaluates to boolean. condition can contain multiple expressions


if (condition) {...} else {...} - allows executing one out of two statements

if (condition) {...} else if (condition) {...} - allows executing one out of many statements

Switch

switch - a construction which executes specific statement(s) based on passed value. those statements

  • break - keyword which forces program to go out from switch
  • if break is omitted, the script will run all the cases followed after the matched one, regardless if they match the value
  • default - a case which is used when no one case matched the value. it can be omitted or can be put between cases
  • alternatives to switch are if...else statement and mapping object
switch (day) {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
    console.log('Another weekday. Enjoy work');
    break;
  case 6:
  case 7:
    console.log('Weekend day! Have a good rest');
    break;
  default:
    console.log('Invalid day');
}

Console

console - is an object which provides access to the browser's debugging console

clear() - clears the console
dir() - displays an interactive listing of the properties of a specified JavaScript object
error() - outputs an error message
log() - for general output of logging information
table() - displays tabular data as a table
warn() - outputs a warning message
assert(assertion, message) - writes an error message to the console if the assertion is false. if the assertion is true, nothing happens
time() & timeEnd() - is used to measure how much time the script takes

  • same string should be passed to both methods

Terminology

keyword - reserved JS word

  • keywords are case-sensitive


variable - container for storing data value

  • variable names are case-sensitive
  • variable name rules:
  • can contain letters, numbers, underscores, or dollar signs
  • can't start with number
  • can't be named same as any keyword


declaration - registering (creation) of variable or function with a given name, in specific scope

  • e.g. let foo; - declaration of foo
  • variable can be declared with const, let, var or automatically


initialization - allocation of memory for variable by JS engine, what makes variable available for access

  • e.g. let foo = 13; - declaration of foo + initialization with 13
  • variables are initialized with a specified value when execution reaches the place in the code where they were declared
  • if no initial value was specified, variable will be initialized with undefined
  • initialization is not develop's work, it happens under the hood. from prospective of developer this is assignment
  • definition - often can be used interchangeably with initialization term


assignment - giving a variable specific value

  • e.g. let foo = 26; - declaration of foo + assignment of value 26
  • assignment operation is always achieved with assignment operator "="


literal - value that appears directly in the code

  • e.g. 3.14


expression - code that resolves to a value

  • e.g. if (expression) { statement } - such expression might contain value or operation, which will resolve to boolean


statement - complete instruction

  • block statement - a multiline statement enclosed in curly braces "{}"
  • e.g.
  • let c = a + b;
  • console.log(c);

MDN

;