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

Fahrenheit to Celsius – JavaScript and AngularJS

You probably know how to convert Fahrenheit to Celsius in JavaScript but how would you display it on a web page?

Google has come up with a modern solution: AngularJS

AngularJS is a way to make your HTML more dynamic and it is easy to implement.

Take a look at my sample here:
http://jsplain.com/javascript/index.php/Thread/45-Fahrenheit-to-Celsius-ng-model/

Have fun testing it!
Tony

Dot Syntax

What is the meaning of the dot?
The dot signifies that we want to use something that’s inside something else.

console.log("log is inside of a console");

myObject.search("Tony de Araujo");
// search is a method which is inside of object myObject

Hope it helps!

A function represents a value

When we look at functions from the same window JavaScript does, things become clearer.

Take for example this function:

 var whatsup = function (){
 return "The sky is up!";
};

JavaScript looks at the function call and it only sees the final product, or what is supposed to return.

Call it like this:

"Do you know what? " + whatsup();

And JavaScript will reply with this:

Do you know what? The sky is up!

So the function was just a placeholder for “The sky is up!”.

By placing the string “The sky is up!”  in a function, we saved it and we are now able to call it at will, since the function is just like a Lego puzzle piece: we can plug it anywhere we want, and it always delivers if we interface it properly.

… Will continue tomorrow, mañana, amanhã, 明天, demain, morgen, 明日kesho …|
(Thank goodness for Google Translate!)

Some JavaScript Storage Concepts

So we want to store data to be used in a script, what options do we have?

Storing simple values:

If the value is single, such as a string or a number, we can just assign it to a variable.
Normally a variable is a pointer, it points to some data in memory. However, in JavaScript, simple values such as strings and numbers are saved in the variable name location, in other words, the variable points to itself like if it was telling this simple data to “stay here near me, don’t go anywhere, you’re too small to wonder around on your own!“.

Storing complex values:

For more complex data we can use an array which is an ordered list, or we can use a hash, also known as an “object” or an unordered list. I placed “object” in quotes because in reality almost everything is an object in JavaScript. I’d rather call this kind of “objects” unordered lists or key-value paired lists.

Now since this data is more complex, the variable pointer stays on one side of the memory ( the stack), and the data stays on the other side of memory ( the heap). The variable points to the data, it does not hold the data. We can actually have several variables pointing at the same data.

Storing scripts such as functionality:

Now if we want to store a program routine, such as to print or to calculate or to fetch some data, etc. Then we use functions. Like any other object, functions can be moved around and placed inside of other scripts. Functions are also great to isolate one script from another script. Moreover, functions are great to help cleaning up the trash in memory because once returned, a function clears itself out (this cleaning is browser dependent since every browser uses a different kind of broom).

And there you have it. If you want a more in depth explanation please read my eBook
JavaScript Objects Functions and Arrays Explained

JavaScript method slice

Today I’m bringing a section from my eBook where we cover the JavaScript library String method slice.

The method slice( ) extracts the text from one string and returns a new string with the extracted text. There is no change to the original variable unless of course the method is applied to itself. This is the same as taking a slice of cake but the cake stays intact. We must give this copy of sliced cake to another variable in order to reuse it.

This method uses up to two parameters. The first parameter indicates the zero based first character to be sliced (copied from). The second parameter indicates the first character to be excluded from the action of slicing. (included from, excluded from).

Sample scripts:

1- Declare a variable:

var xpt = "red,blue,white,black";

2- Extracting the word “blue” into another variable:

var xpt = "red,blue,white,black";
var xpt2 = xpt.slice(4,8);

3- Call xpt2;

It returns “blue”. The first character to be included is on position 4 ( as in 0,1,2,3,4), and the first to be excluded is on position 8, which is the comma.

Using negative numbers:

As an alternative, if we apply a negative number to the second parameter, it changes its meaning: now it means to start excluding from the right, based on the number of characters indicated. Commas and spaces count as characters.

4- For example: excluding the last color (black and the comma to its left) from our slicing):

var xpt = "red,blue,white,black";
var xpt3 = xpt.slice(0,-6);

5- Call the variable: xpt3;

It returns “red, blue, white”. In other words, it includes all characters from position zero on, except the last 6 from the end. This also means that we could exclude some characters from the extreme left, if instead of zero we entered some other number as the left parameter.

Using just one parameter:

When using just one positive parameter, this parameter represents the first character position to be included on the slice which extends all the way to the end.

1- For example:

var xpt = "red,blue,white,black";
var x = xpt.slice(3);

// returns “,blue,white,black”, which means that “red” was cutoff.

If the single parameter is negative, it has an opposite effect: it tells JavaScript how many characters to include from the right and exclude all else.

2- Example:

var xpt = "red,blue,white,black";
var x = xpt.slice(-3);

// it returns “ack” from “black”. It counted from the right to the left 3 characters and cutoff all the others at the left.

3- One more example. Extract the “e” from ecmascript:

"ecmascript".slice(1);

// returns “cmascript”.
This is actually useful to remove the first character and replace it with another similar character capitalized, as we will see later in the eBook