JavaScript new operator examples

The javaScript new operator is something that will come up in the occasional code example here and there. So knowing a thing or two about what the new operator does, and being aware of the many other subjects that branch off from it, is a must for any javaScript developer. It is also a good idea to know what also happens when one does not use the new operator when calling a function also, and that functions in javaScript can be designed in a way in which they will work well and as expected both with and without the use of the new operator when calling them.

A constructor function is a way to create a function that will constructor a type of object that contains methods and properties that are not unique properties and methods of the object itself, but are contained in the prototype object of it. If you have logged any about of time at all playing around with javaScipt chances are you might have all ready used the new keyword with a built in constructor such as the Date constructor for example.

In this post I will be touching base with some examples that make use of the new operator, and some related subjects to the use of the new operator and constructor functions that are what the new operator is often used with.

Read More

=, == , ===, and Object.is in javaScript

So the javaScipt == or Equality operator is used to find equality in expressions. However there are a number of other options to be aware of in javaScript in addition to the equality operator there is also the === or identity operator that is used to preform a more strict, type sensitive kind of comparison between two values. There is yet even another option that comes to mind that is not an operator, but a static method called the Object.is method that brings yet even another standard to be aware of. However none of these should be confused with just a single = operator, that is used for assignment, and there is really only that when it comes to doing so.

So there is more than one comparison operator in javaScript because of javaScripts typeless nature. This might case some confusion, but if you take a moment to just work out some simple examples that confusion can be quickly dispelled.

A variable can be of any kind of type at any given moment, at one time it can be the number 5, and at another moment it can be the string 5. So there is a need for a comparison operator that does type conversion, and another that does not. This is a subject that comes up often in javaSciprt related discussions so it goes without saying that I should write a post on this one. So then in this post I will be going over a few quick examples of comparing to values to each other that might be equal to each other in javaScript.

Read More

Operator precedence AKA order of operations in javaScript

When writing javaScript expressions knowing the order in which operations are performed is important to make sure that desired results will always be achieved, this is often called operator precedence, or just simply order of operations. Each type of operator has a kind of precedence or level of importance compared to others, as such operators of higher precedence are performed before operators of lower precedence. In addition to this operator precedence there is also associativity of operators as well, that is the direction from left to right or the inverse of that when it comes to performing operations.

For example, multiplication is always performed before addition, and a lengthy expression that is contained within a grouping operator is performed before any additional operations that are to be performed outside of it. Many operators are performed from left to right when it comes to associativity, but this is not always the case with many others. So then in javaScript operator precedence, and associativity is something that a developer should have at least some grasp on as it will come up when authoring or studying expressions in a project. So there is the question of what operators are performed first (operator precedence aka order of operations), and then also the direction in which they are performed as well ( associativity ).

In this post I will be going over many examples of order of operations in JavaScript that should help with gaining at least a basic understanding of that topic, as well as associativity, and maybe some other little things here that I will likely branch off into when it comes to writing some actual real functioning examples of order of operations and javaScript code.

Read More

JavaScript Call object, local variables, and the Function prototype

The Call object in javaScript is used as a way to store local variables, this call object contrasts with the global object which is the top most name space where variables are stored. So the call object is a way to help keep the global name space from becoming polluted by giving javaScript developers a way to have a separate collection of variables that are only local to a functions call object. Another term for the call object in javaScript would be the activation object, this term might also be used as a way to eliminate confusion with the call function prototype method.

What is gained by way of a js call object is of course on top of the block scope that also helps with reducing the changes of name space collisions, and pollution of global variable space. Block variable scope is however a feature of javaScript that was introduced in modern specs of javaScript, in the bast javaScript had function level only variable scope.

So in this post I will be writing about the call object, and function scope local variables in javaScript. Also I will be at least touching base on some related topics such as the Function call prototype method, and the nature of the this keyword in core javaScript while I am at it.

Read More

JavaScript global variables and the global object

In javaScript global variables are variables that can be accessed from anywhere within a body of javaScript code, and are therefor at the global name space. In most environments global variables are also part of what is often called the global object, in client side javaScript this is typically the window object. However that can not be the case when it comes to getting into web workers on the front end, but that is a matter for a whole other post.

I hear many developers saying that the practice of creating globals is something that should be minimized, if not completely avoided all together if possible. One reason why is because of the possibility of writing over something else that is in use by setting something to the same variable name. Still with simple projects at least I find myself using them just for the sake of getting something together quickly. I often will wrap everything into a closure though when things start to get more advanced, or if it is time to create a single package for deployment rather than more readable source code.

In this post I will be writing about some things to be aware of when dealing with global variables, as well as the alternative which would be local function level, and now block level scoped variables in ecma2015+ spec javaScript. When it comes to the use of let and const over the tired yet true var keywords when declaring variables we are now no longer limited to function only variable scope.

Read More