JavaScript Objects Functions and Arrays Explained

How many times have you attempted to learn JavaScript, only to put it on the side because it was too complex or you felt overwhelmed by the information overload?

JavaScript Objects Functions and Arrays Explained is easy to understand with plenty of illustrations and links to code files you can physically copy. Easy does not mean simple, it will not insult your intelligence and capacity to learn new concepts. This is a serious book for those wanting to understand how JavaScript works.

This book can be used as a reference if you need a detailed explanation on how a certain method works.
The key to master JavaScript is to become familiar with its library methods.

Get it today and start learning. You don’t need to own a Kindle. You’ll be able to read it on anyone of your computers by downloading the free Kindle app.
–> JavaScript Objects Functions and Arrays Explained

book_object

Finding the smallest factor of a number with JavaScript

Time to practice some code!

Create a function named smallestFactor that finds and displays the smallest factor of any number (num).

Then call the function by passing in the value of  100. It should display the number 2 as a result.

Whenever you’re ready to look at my examples please refer to
http://bit.ly/1sDxC2O

Tony

JavaScript’s Conditional Operator

The conditional operator is a one liner if/else statement. Another name for it is ternary operator because it uses three operands:

expression1 ? expression2 : expression3;

If expression1 evaluates to boolean true, expression2 gets executed. Else, expression3 gets executed instead.

If you’re like me, you probably prefer to use if, else if, else statements, but the ternary operator can be very handy at times. One of the things we can’t do with our regular branching techniques is to include alternative outputs on a one console.log statement, but with the ternary operator we can:

var x = 10;
var y = 9;
console.log(x < y ? "hello!" : "goodbye!");
// it outputs "goodbye!"
console.log(x > y ? "hello!" : "goodbye!");
// it outputs "hello!"

Cool thing!

A Mind For Numbers: How to Excel at Math and Science

If you want to understand some of the most important secrets to learning math and science, look at this book.

Whether you are a student struggling to fulfill a math or science requirement, or you are embarking on a career change that requires a higher level of math competency, A Mind for Numbers offers the tools you need to get a better grasp of that intimidating but inescapable field. Engineering professor Barbara Oakley knows firsthand how it feels to struggle with math. She flunked her way through high school math and science courses, before enlisting in the army immediately after graduation. When she saw how her lack of mathematical and technical savvy severely limited her options—both to rise in the military and to explore other careers—she returned to school with a newfound determination to re-tool her brain to master the very subjects that had given her so much trouble throughout her entire life.

The JavaScript Date Object

The JavaScript library has an object called Date. Unlike for example the Math object, the Date object methods cannot be accessed in the Date object raw state, we must instantiate a sample of the Date object to work with, almost like getting a license to use its methods on our script.

If we call Date by itself…

Date();

JavaScript returns today’s complete date, like for example “Sat Jun 21 2014 16:02:33 GMT-0400 (Eastern Daylight Time)“.

Creating a working Date object instance.

We create an instance of Date with the new keyword, like for example:

var sampleDate = new Date;

Access your Chrome or Opera JS Console by pressing CTRL SHIFT j and try it yourself.

Once we have an instance of the object Date we can use it as an interface in order to access Date methods from the JavaScript library:

sampleDate.getDate();
// returns today's monthly day. Example: 21
sampleDate.getFullYear();
// returns 2014

The idea of a blog is to be brief and to the point. However, in my JavaScript Objects Functions and Arrays Explained eBook I write in extensive detail about these methods and how to use them in real life.

See you there!

Calling or call(ling) a function in JavaScript

What’s up?” That was the topic of my last post when we used the line to illustrate how JavaScript sees functions as an end result. Functions are created to save functionality and JS sees them as the outcome of acting upon such functionality. We may tell JavaScript that we see a brownish round piece that looks just like a potato and returns “that is a potato!”, and JavaScript may look at us with dismay and think “What’s wrong with this person? I only see ‘That is a potato!‘”.

Today I’m going to shift gears a bit to talk about calling functions as opposed to creating them.

Let’s see how a function is called in to do its magic. Here’s a simple function that adds 1 to any number you pass in when you call it:

var addOne = function (num) {
  return num + 1;
};

Call it and pass the number 3:

addOne(3);

It returns 4. The way we called the function on line 5 is actually a shortcut for the following line:

addOne.call(window,3);

Let me explain: call() is from the JavaScript library. It’s function is to call an object on the first parameter (in our example, window), pass in some data on subsequent parameters and apply this data to the object attached to .call(), which happens to be our function addOne.

As a matter of fact, the full path would have been the following:

window.addOne.call(window,3);

Why? Well, addOne is a function that belongs to the global object we know as window. So, we are telling JavaScript to call the function addOne which belongs to the browser window, and pass in 3, which is a number that also belongs to window. In truth, the second window on the right is only there to fill in that first parameter because in this case we only want the second parameter, 3,  but since we have to insert a placeholder there, might as well do it with the correct object.

Thank goodness for shortcuts because all we have to do is this: addOne(3); instead of all that mumbojumbo.

But make not mistake, read this article carefully because call may not be needed in our example, but if you understand this example, you are well on the way to understand advanced calling methods.

If this subject interests you please read my eBook
JavaScript Objects Functions and Arrays Explained

Functions with names versus anonymous functions

Anonymous functions are not really as anonymous as they look at first sight.
Take for example the following function:

var x = function () {
  return 10;
};

Is this an anonymous function or is it a function named x?

The answer is both. Actually this is a variable named x that points to an anonymous function.

There is no difference between x = 5; or x = “tony”;, or x = function() {…

All these are variable assignments. As a matter of fact if we fire up our Chrome/Opera/Epic consoles ( CTRL SHIFT j) we can test it.

  1. Paste the above function into the console.
  2. Then type x;  to see what it prints out: It prints out the anonymous function explicitly.
  3. Now make this function a self-invoking function by adding a pair of parentheses to the end of it:
    var x = function () {
      return 10;
    }();
    
  4. Let’s check what x is assigned to by typing x;
    Now x is assigned to 10, not to the function itself but to the end result of function execution.So when we look at functions as their end result assigned to some variable suddenly they start to make more sense.

I go into more detail about self-invoking function on my latest eBook:
JavaScript innerHTML: A Project in Three Acts (Small Projects for Mastering JavaScript)

Tony