Code Snippets Events

Javascript Events

In this code snippet, we’ll learn about events in Javascript. We can add and subsequently remove event listeners(of different types: click, change, mouseenter, …) to any element of our choosing. These event listeners will wait for the specified event(like clicking on an element) to occur. When finally it does the event listener will call/execute the code we specified beforehand. 

JS Code Snippets DOM Manipulation

Javascript DOM Manipulation

In this code snippet, we’ll learn how to manipulate the DOM with Javascript. When you open up a web page the browser will parse the HTML and CSS. Then it will build a DOM(data object model) from the parsed data. Finally, it will render it to display the web page. Javascript can be used to dynamically change the DOM. We can do that by getting elements(by tag, id or class) and then modifying, adding or removing them.

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#).

Advertisment ad adsense adlogger