JavaScript undefined value and keyword what to know

In javaScript undefined is a value that comes up often when working with various code examples, and projects. For one thing the undefined value is the default value for variables that are declared, but do not have any value assigned to them. In addition if I attempt to access an object property value that is not defined, then the result is undefined. However an object key and a variable can be both declared, and intensionally assigned the value undefined bu using the undefined keyword, or preforming any kind of action that will result in the value of undefined being set to the variable or object property.

If I attempt to call an object property that I expect is a function, but turns out to be undefined, that can result in an Error that is the result of calling undefined. This can often be the case when choosing to go with function expressions rather than declarations and neglect to do what is required to keep that from happening, or it could just be a simple typo.

When working with functions a value of undefined is what is returned by a function by default unless something else is returned by using the return keyword. This might be a good thing in some cases as undefined will evaluate to false, so when it comes to functions that return a boolean value it might not always present a problem. Still it might be a good idea to have the function return false anyway just for the sake of making things explicit.

There is also the undefined keyword that can be used to intentionally set a variable to undefined, and can also be used in expressions when it comes to testing for undefined. That is that I often find myself using the undefined keyword as a way to test for undefined by combining the undefined keyword with an identity operator and a value that is to be tested for undefined.

So chances are if you have been fiddling with javaScript for at least a little while, are you have come across undefined a few times all ready. However there is much to be aware of when it comes to this value in javaScript, not just with the value itself, but of course with many things in core javaScript that branch off from it. For example there is the fact that undefined will evaluate to false, and that the undefined value is just one of many other values that will do so. So in this post I will be outlining some examples that point out some things that a javaScript developer should be aware of when it comes to undefined in javaScript, and I am sure that I will at least touch base on all kinds of other things in the process of doing so.

Read More

Variable scope in javaScript

The variable scope of a variable in javaScipt is the area in code where the variable is defined, and also where it can be accessed elsewhere in code.

If a variable is inside the scope of a section of code it is of use there, else it can not be accessed. There is for example variables that are defined at the top level of code that are often called global variables. These kinds of variables can be accessed anywhere else within a body of code for a window object in client side javaScript, or within a module for nodejs. This concept of global variables compares to variables that are declared inside the body of a function with var, or inside blocks of code when it comes to let and const. With var there is function level scope, and with the newer let and cost options there is block level scope. These kinds of variables are often called local variables. Traditionally javaScipt had function level scope only with the var keyword, but these days there is block level scope as well via let and const.
In this post I will be going over some of the ins and outs with javaScript variable scope both with the way it was, and the way it is now. There is a fair about to write about when it comes to this topic when it comes to things like the scope chain, and things can quickly branch off into other subjects such as closure.

Read More

function expressions in javaScript

There are many ways to go about defining a function in javaScript one such was is by writing Function expressions, which are also sometimes called function literals. A function expression is a way to define a function as an expression rather than a statement, or declaration which is another traditional way of defining a function.

There are even more ways to go about defining a function on top of just expressions and declarations in modern specs of javaScript there are now arrow functions which often prove to be a more concise way to define a function, however the this keyword is handled differently so it is not always a drop in replacement for expressions and declarations. There is also using the function constructor to create functions my way of passing a string of javaScript code for the function to a constructor, the returned result is then a new function. I can not say I create functions that way often, but it is yet another way to do so. However this post is mainly just on function expressions, so I will not be getting into those in detail here.

Function Expressions have some advantages over function statements or declarations, but they are also not necessary a full replacement for function statements. There are also may other ways to create a function in javaScript such as array functions and the Function constructor. However in this post I will be going mainly over some of the ins and outs of function expressions in javaScript, and why they can come in handy now and then.

Read More

document location object in client side javaScript

The location property of the document object in client side javaScript contains a location object. This location object contains the URL of the current page, along with other useful properties about the current location of a web page. So the property is useful for finding out where a script is being used, but it can also be used as a way to redirect to a new page by setting a value to the href property that is the new desired page url.

So then in addition to being a way to know the current URL, it can also be used to preform a redirect to a new location by setting the value of the document location property to a string that is the new URL to go to. That is because although the object itself is read only a new URL can be set to the property that will cause the browser to load that new given URL that it has been set to. So there is using this location object to know the current location, and also using it to set a new location.

So the location property of the document object will come in handy when making any kind of client side system that needs to know the current location where it is running, as well as changing what the current page is with client side javaScript. With that said in this post I will be outlining some basic use case examples of the document location property as well as some other related topics such as window location and how it compares to document location.

Read More

javaScript Strings must know methods and more.

A javaScript String is one of the primitive values that there are to work with when working in a javaScript programming environment. A string is a collection of characters that compose text, and as such can serve a number of purposes beyond just simply displaying human readable text.

Strings are a type of primitive value rather than an object, however there is a wrapper object to work with when it comes to a string which can give the allusion that a string is a kind of object. The wrapper object of a String is loaded with all kinds of useful methods to help worth with a string value. In addition the String wrapper object is array like, so often many array prototype methods can be used with a string by way of something like the Function call prototype method.

There is a great deal to write about when it comes to javaScript Strings, but in this post I will be going over just some of the basics of strings including some of the must know String prototype methods. In the process of doing so I might manage to cover some general quirks to look out for when working with a String in javaScript, and I might also branch off into some closely related topics where doing so might be called for.

Read More