javaScript return statement

The javaScipt return statement is used in the body of a function to return a product when the function is called. This returned value can then be stored into a variable, or additional methods in the prototype of the value that is returned can be called off of it to returned yet another value. In addition the value that is returned can be a function, and this internal function can have access to the variable scope of the other function in which it is contained, a concept known as closure.

The product that is returned can just be a simple primitive value such as a number or string, but things get more interesting when it is an object, or a function. The return statement can also be used as an alternative to the break keyword in the body of a function if looping is no longer required, and as stated is also an important part of creating closures. So the return keyword is something that a javaScript developer should get a solid grasp on, and maybe the best way to do so is to not just read a post such as this, but also just start playing around with some own code examples when it comes to the whole learn by doing thing.

In this post I will be exploring some examples that have to do with the return statement in javaScript and touch base on some related topics surrounding the javaScript return keyword as well. I will not be getting into things like closure in detail here of course, but it is called for to touch base on a lot of these things at least here. So I will have to have at least one section in this post where I am going over some examples of closures. However this will mainly be just a post on the return keyword in general.

Read More

The javaScript Constructor Function, Classes, alternatives and more

One kind of function that is close to the core functionally of javaScript is the concept of a constructor function. A constructor function is a kind of function in where the new keyword is used when invoking it to make it so that the value of the this keyword inside the body of the constructor function will refer to a new instance of a Class of an object that will be returned. One major advantage of using these kinds of functions is making use of the prototype chain as a way to make certain methods and properties part of an object that will be a fall back object of sorts from the own properties of an instance of the class object. So in this post I will be touching base on the subject of constructor functions, the use of the new keyword, and other related subjects that surround the use of constructor functions such as the this keyword, the prototype chain, and all kinds of various things that surround functions in general in a javaScript programing environment.

With that said in javaScript there are many types of functions beyond just that of constructor functions in the sense of how to make one in javaScript to begin with, such as arrow functions, function expressions, function declarations, just to name a few such options. However there are also many ways that functions can be used to create different kinds of functions that are independent of a specific language as JavaScript, or programing idiom such as object oriented programing. Some examples of what I mean by this would be pure functions, monotonic functions, and inverse functions just to name a few that come to mind when it comes to going down that rabbit hole. So there is much to learn about functions in general in javaScript, not just with the language itself, Classes and Constructor functions of such Classes, but topics that surround functions in general also.

There are built in examples of constructors that chances are you have at least some experience with. One such example would be the Date constructor that when used with the new keyword will return an instance of a date object. This date object is not just an object, but an instance of the Date class, as such there are a whole bunch of methods that can be called off of the date object instance. An example of one of these prototype methods with respect to the date class would be the getFullYear method that will return the full year of the date object. There is not just using built in classes, but also knowing how to go about create ones own classes from the ground up, and the way to start doing so would be to write a constructor function.

There is the traditional way of creating a javaScript constructor function, and then the more modern es2015+ spec javaScript way of making them as well with the class keyword. In this post I will be covering the basics, as well as some other aspects of constructors that a javaScript developer should be aware of when working with, and creating these types of functions in javaScript. I will also be looking into some ways to go about not making classes actually that some developers like to do, preferring to go more in a functional programing style direction with code rather than the Object Oriented Programing nature of constructors.

Read More

Add elements with javaScript

The process of Adding elements in javaScript generally refers to creating and appending html elements to a container element in an html document. Also in core javaScript by itself in general, adding elements may refer to creating and appending elements for an Array. However in this post I will be going mainly over ways to go about adding html elements to an html document when it comes to client side javaScript.

In jQuery there is the add method that can be used as a way to add elements. It is true that jQuery is still a widely used front end javaScript framework, but it is a library that is starting to die out a little. There are many other front end frameworks that make adding elements, and many DOM mutation related tasks a little easier but today for the most part just working in the browser itself is not so hard and can still work just fine for small projects.

I have written a post on innerHTML not to long ago which is one typical way of adding elements once a reference to a container element is obtained. However in this post I will be covering the subject in general, and not just the use of innerHTML which might not always be the best choice. There are of course alternatives to innerHTML that involve the use of a collection of methods, such as createElement, and appendChild.

Also there is some things to cover when it comes to html node lists as well, and how they are like arrays. However they are not instances of the Array constructor so one can not just use array prototype methods with them. So there is a little to cover when it comes to adding elements to an html document, and everything else that might branch off from this topic such as removing elements.

Read More

If statements in javaScript, and other control flow stuff

In this post I will be writing about javaScript if statements, and other related concerns when working with conditionals in general in a JavaScript programing environment. In many programing languages, in fact just about all of them actually an if statement can be used to check if a certain value, or expression evaluates to a true boolean value value, and in the event that it is true, run some code that would otherwise not run. Thus an if statement is a kind of control flow statement along with other options that come to mind such as switch statements, loops, functions, and other clever ways of controlling the flow or execution of code.

So then in addition to if statements there are also switch statements in javaScript that can also be used as a kind of control flow structure. In addition to these two options there is also yet event more options such as a conditional operator as well that can be used as a short hand for if else statements for example, which is also often very useful when it comes to write expressions. Yet even more tools in the tool box that can be seen as a form of control flow would be various kinds of loops such as while and for loops, and various kinds of structures such as a state machine and so forth.

If statements are a fundamental component of javaScript code, or in any programing language for that matter. So having a solid grasp on how to go about using them is key to writing just about any kind of project. There are additional tolls to work with in javaScript that are also worth mentioning when it comes to controlling the flow of a program other then that of if statements, such as switch statements, or doing something to compartmentalize code into groups using functions and objects. So lets look as some basic examples of if statements in javaScript, and many some additional related things while in the process of doing so.

Read More

javaScript closure examples

There are a number of subjects that some javaScript developers might considered an aspect of advanced javaScript, one such subject might be the subject of closures. When it comes to the question of what a closure is to begin with there are many ways to go about defining what a closure is, which right off the bat can lead to some confusion. Some definitions are very simple, yet technically still correct, however they might not help to give the full picture of what a closure is and why they are useful in many situations that will pop up when working on a project. Other more complex definitions are a bit of a mouth full but do a better job doing them justice when it comes to truly understanding them, and what their full potential may be when keeping them in mind as an option.

There are all ready many posts on the open web on the subject of closures in javaScript, just about any javaScript developer that writes a blog will likely get around to writing at least one post on them sooner or later. It is just on of those topics like the this keyword, pure functions, and the nature of prototype inheritance when it comes to working with classes. So it was only a matter of time until I wrote this post also, so as such, here it is.

So then today I will be looking into closures in javaScript with some basic, and maybe not so basic examples of them. While doing so I might get around to touching base on some related topics that have to do with pure functions, classes, and other ways of storing a state.

Read More