JS Code Snippets Closures

Javascript Closures

In this code snippet, we’ll learn about closures in Javascript. A closure is simply a function within a function. With this, we can achieve variable encapsulation. In other OOP languages, this would be done by making a private property inside a class. This way the variables inside are isolated and protected from the global space.

JS Code Snippets Arrow Function

Javascript Arrow Function

In this code snippet, we’ll learn how to use arrow functions in Javascript. Arrow functions are nothing more than “syntactic sugar” to make writing a function a bit shorter. Both examples below achieve the same thing but the second one is just a bit more compact that’s all.

JS Code Snippets Error Handling

Javascript Error Handling

In this code snippet, we’ll learn how to handle errors in Javascript. You can throw errors by using the throw keyword. Then you can catch them using the try catch block. If you want to catch errors globally you can use the window.onerror event to call your error handling function.

JS Code Snippets Classes

Javascript Classes

In this code snippet, we’ll learn about classes in Javascript. Classes were introduced ES6. They work the same way they do in other languages and act as templates to make objects from. Again I will just demonstrate how to use classes in javascript and won’t go into specifics of OOP(I already did some posts about it in C#).

JS Code Snippets Objects

Javascript Objects

In this code snippet, we’ll learn about objects in Javascript. Objects can contain other objects, functions, properties and act as a sort of container for them. This is useful for transferring/storing data, modeling real world objects and for grouping a certain set of related functions and variables into one place.

Advertisment ad adsense adlogger