JavaScript
Unique traits
-
obj.propKey = valueandobj['propKey'] = valueare 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 / awaitThis component was made by Stratis Dermanoutsos. The code can be found here. -
thenThis 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
-
async / await, withtry / catch / finallyThis component was made by Stratis Dermanoutsos. The code can be found here. -
then / catchThis 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.
The above code is a simple fibonacci implementation. The result is calculated each time the function in called.
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
Filter out falsy values
Swap two variables with array destructuring
Destructure function parameters, making code more readable
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
Optional chaining
C# vs JavaScript
| C# | JavaScript | |
|---|---|---|
| types | int, float, double, char, bool, ... | let, var, const |
| methods | <access> <type> <name>(<type> x) | function <name>(x) |
| named parameters | fun(x=4) | fun({ x: 4 }) |
System.Console.WriteLine(str) | console.log(str) | |
| str -> num | int.Parse(str) | +str (or parseInt(str)) |
| num -> str | num.ToString() | num.toString() |
| exceptions | try {} catch(Exception) {} | try {} catch(exception) {} |
| closures | return delegate() { return val;}; | return function() { return val;} |
| foreach | foreach (int elem in arr) {} | for (var elem in arr) {} |
| using | using (obj) {} | with (obj) {} |
| class | class Dog { string name; public Dog(string x) { this.name = x; } } | function Dog(x) { this.name = x; } |
| inheritance | class Subclass : Baseclass | var subclass = { [[Prototype]]: baseclass } |
| accessors | <type> X { get {return x;} set {x = value;} } | get X() { return x; }, set X(value) { x = value; } |
| regular expressions | Regex reg = new Regex(@"^\w") | /^\w/ |