JavaScript

Unique traits

  • obj.propKey = value and obj['propKey'] = value are equal

  • if 1 = 0001, then ~1 = 1110

  • 'abc'.charAt(1) and 'abc'[1] are equal, tho some older browsers don’t support the latter

  • Numeric Separators are useful to make large numbers more readable

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Logical OR can be used for falsy values

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Nullish Coalescing

    This component was made by Stratis Dermanoutsos. The code can be found here.

    Falsy values are kept.

  • Grab last item of an array

    This component was made by Stratis Dermanoutsos. The code can be found here.

Comparing strings

  • is case-sensitive

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • doesn’t handle umlauts and accents well

    This component was made by Stratis Dermanoutsos. The code can be found here.

Object cloning

There are 2 general with which you can clone an object.

  • By reference

    This component was made by Stratis Dermanoutsos. The code can be found here.

    This is not a deep clone.

    Changing any property of the clone will also change the original object.

  • By value

    This component was made by Stratis Dermanoutsos. The code can be found here.

    This is considered to be a deep clone.

    It’s a completely different object and changing its properties will not affect the original object.

Asynchronous code

There are 2 main ways:

  • async / await

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • then

    This component was made by Stratis Dermanoutsos. The code can be found here.

Generally it is best to use error handling for both cases.

For the sake of this example, we’ll take a simple promise

This component was made by Stratis Dermanoutsos. The code can be found here.
  • async / await, with try / catch / finally

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • then / catch

    This component was made by Stratis Dermanoutsos. The code can be found here.

Required arguments

Make a function that, when assigned to a parameter, throws an error saying it is required.

Now, when anyone calls myFunction without assigning a value to the param argument, it throws an error.

Console API

There are many ways to output something using the console API.

  • Normal log

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Error message

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Warning message

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Measure time

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Table

    Let’s say we have an array of objects

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Trace

    Use this to see how the output was reached

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • Better object output

    This component was made by Stratis Dermanoutsos. The code can be found here.

Memoization

Memoization is an optimization technique, to reduce the complexity of the application, runtime of the application, and proper utilization of resources (Time and Memory).

What this means? It means that if you have a function that takes a long time to execute, you can store the result of the function in a variable and return that variable instead of executing the function again.

This component was made by Stratis Dermanoutsos. The code can be found here.

The above code is a simple fibonacci implementation. The result is calculated each time the function in called.

This component was made by Stratis Dermanoutsos. The code can be found here.

The above code is the same as the previous one, but with memoization. As we call it 5 times, the result is calculated only once in the first call and then returned from the cache for the rest.

Useful code snippets

Remove duplicate values from array

This component was made by Stratis Dermanoutsos. The code can be found here.

Filter out falsy values

This component was made by Stratis Dermanoutsos. The code can be found here.

Swap two variables with array destructuring

This component was made by Stratis Dermanoutsos. The code can be found here.

Destructure function parameters, making code more readable

This component was made by Stratis Dermanoutsos. The code can be found here.

Short circuit Evaluation

  • AND

    This component was made by Stratis Dermanoutsos. The code can be found here.
  • OR

    This component was made by Stratis Dermanoutsos. The code can be found here.

Convert result of division to integer

This component was made by Stratis Dermanoutsos. The code can be found here.

Optional chaining

This component was made by Stratis Dermanoutsos. The code can be found here.

C# vs JavaScript

C#JavaScript
typesint, float, double, char, bool, ...let, var, const
methods<access> <type> <name>(<type> x)function <name>(x)
named parametersfun(x=4)fun({ x: 4 })
printSystem.Console.WriteLine(str)console.log(str)
str -> numint.Parse(str)+str (or parseInt(str))
num -> strnum.ToString()num.toString()
exceptionstry {} catch(Exception) {}try {} catch(exception) {}
closuresreturn delegate() { return val;};return function() { return val;}
foreachforeach (int elem in arr) {}for (var elem in arr) {}
usingusing (obj) {}with (obj) {}
classclass Dog { string name; public Dog(string x) { this.name = x; } }function Dog(x) { this.name = x; }
inheritanceclass Subclass : Baseclassvar subclass = { [[Prototype]]: baseclass }
accessors<type> X { get {return x;} set {x = value;} }get X() { return x; }, set X(value) { x = value; }
regular expressionsRegex reg = new Regex(@"^\w")/^\w/

Resources