Posts for Javascript

JavaScript, often abbreviated as JS, is a high-level, interpreted scripting language that conforms to the ECMAScript specification. JavaScript has curly-bracket syntax and dynamic typing

Getting Started with the Media Session API

The Media Session API enables web apps to provide a way to interact with media elements (audio, video) without actively being on the web page playing the media. All you need to achieve this is to provide some metadata, event handlers, plug them to a media element and have rich media experience. This allows users […]

February 15, 2020 in Javascript & Tutorials

Building a Word Counter in JavaScript

There are various reasons a web application may want to collect input from its users. However, there are times you want to limit how much input you get from your user. In this tutorial, we’ll learn how to create a word counter in JavaScript and stop the user from entering any further information when they […]

February 13, 2020 in Javascript & Tutorials

Avoiding side effects in JavaScript code

As developers, we tend to spend more time debugging our code than writing new features. Most times, this is due to the negligence of the side effects in our codebase. In this article, we will understand what side effects are and how side effects can be avoided. In programming, a function is a unit of […]

February 3, 2020 in Javascript & Tutorials

An Introduction to JavaScript Closures

Javascript provides the functionality of nested functions, returning a function, etc. These nested functions have access to the scope of its parent function.Consider the snippet below: function counter() { let value = 0; return function(incrementer) { value += incrementer; console.log(value) } } let myCount = counter(); myCount(4); // 4 myCount(4); // 8 In the above […]

January 28, 2020 in Code examples & Javascript

Improved Control Flow In JavaScript

This is nothing new in the language as this concept does the same thing that the normal if, else-if and else statement does but with a neat and less verbose syntax.  However, there are some cases that will be much easier to handle with the advanced control flow than the if, else-if and else statement. […]

January 27, 2020 in Code examples & Javascript

Regular Expressions (RegExp) in JavaScript

When working in any programming language, you may want to search characters within strings most of the time. It’s very easy to perform the following check; But, what you are searching for may not be equivalent to the entire content of the string. It may just be part of it – at the beginning, or end or just somewhere. In order to achieve this, Regular Expressions (short form – RegEx or RegExp) are used. What are Regular Expressions? These are sequence of characters which define a search pattern that can  be used on strings. How to Create a RegEx There are two methods involved in creating regular expressions which are: 1. Calling the constructor function of the RegExp object 2. Using a regular expression literal As seen above, the expression is declared between the slashes for literals while for the constructor function, the expression is declared as an argument of type string. The major difference between these two syntaxes is that expressions (like string template literals) can be used in the constructor function but not in the literal expression because it is fully static. Literal expressions are used when the regex is known before execution. But the first syntax is used when there is need to create a reg during run time (such as after taking user input). The expression ^jav matches strings which begin (^) with “jav“. We’d be looking at more ways of declaring expressions later in this article. Application of Regular Expressions Expressions can be used with test() and exec() methods of the RegExp object. test(): Here, you can use the expression to test a string. It returns true if it matches the string or false if otherwise. exec(): This method returns an array of information (index, input and group) and updates the lastIndex property of the regex object if it matches the string. Otherwise, it returns null. It can be used to search for multiple matches in a string. For these methods, the syntax for application is regex.method(string) . The method is applied to the declared expression and takes an argument of the string which it is tested on. Regex can also be used with match(), search(), replace() and split() methods of the String object. match(): If it matches, it returns […]

January 16, 2020 in Javascript & Tutorials

Trim() Method for Strings in JavaScript

The trim() method is applied on strings to remove whitespaces from the beginning and end of the string. Whitespaces include space, tabs, no break-spaces and so on. Syntax string.trim(); The method does not take any argument. It is called directly on the string. Return Value The return value of this method is the same string […]

January 7, 2020 in Code examples & Javascript

Understanding scope in JavaScript

JavaScript is a simple, partially easy to use language with broad attractions in the web development industry. Unfortunately, it is built upon a complex collection of language mechanics which without careful study will not be understood even by seasoned developers. It is even harder to learn JavaScript completely. In this article, we will focus on […]

December 29, 2019 in Code examples & Javascript

Understand Promises in JavaScript

You know that moment you promise to send your friends a Christmas gift and they have to wait for you to keep that promise(resolve), then further actions can be taken or if you break the promise(reject), they would like to know what happened (catch), so that they can plan what to do next or how […]

December 26, 2019 in Code examples & Javascript

Applying SOLID principles to an existing application

This is the last part of the SOLID Series. We are going to refactor the CLI application built in the first part. To follow on with this part, you must have read the previous tutorials: Building a CLI application in Javascript. Understanding SOLID principles. Discovering Defects To refactor the CLI application, we need to first […]

December 24, 2019 in Javascript

SOLID Principles of Object-Oriented Design

There’s a bunch of principles in object-oriented programming. One of them and probably the most popular is SOLID. SOLID is an acronym for:Single Responsibility PrincipleOpen/Closed PrincipleLiskov Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle The SOLID principles were coined by Robert C Martin (Uncle Bob). Single Responsibility Principle This is a principle that attempts to provide a […]

December 24, 2019 in Javascript