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:

Have fun testing it!

JavaScript value and reference variables

Are you still puzzled by JavaScript terms such as typeof,
reference variables, value variables, stack vs heap?
My new article at JSPLAIN (JavaScript in Plain Language)
is short, sweet and right to the point.
Here’s my link: http://bit.ly/1wz2rLq
Have a great weekend!!

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!

forEach, a way to manipulate arrays

The array method forEach( ) was introduced with ECMAScript 5 when nine new methods were added to help developers create ways of searching and manipulating array contents. These methods perform operations that previously would require complex iterative loops. The nine additional methods are: indexOf, lastIndexOf, every, some, forEach, map, filter, reduce, reduceRight. All these methods are variations of the forEach() method which I’m about to discuss.

forEach( ) performs a specified action for each element on the array. The specification of what to do is written by the programmer as a function in the form of an argument to the forEach() mechanism.

Remember, the purpose of a function is to store some code that will run when the function is called. We create the function, and forEach() will process it.

So, forEach( ) takes a function as its argument. The parameters of the function itself will interface with each valid element of the array in an iterative manner, just like a loop would do. By valid element I mean an element with data ( not an empty element location). Array indexes not having a value, or the value being undefined (if any), are ignored.
1- Let’s experiment with this method in increments. Start by introducing on the console a new array (I’m using Chrome):

var g = ["red", "blue", "white"];

2- Call forEach( ) on the array g by using a function with just one parameter, “y” , and in the function we stored a simple console.log output:

g.forEach(function(y) {

The function being used here may contain up to 3 arguments. The first argument (as the example above), points to the array data. In the forEach( ) example given above, the parameter y will point to the value of each index on the array, and the method forEach will process each value according to the instructions on the body. In this case the next instructions are to console.log each value at each “y” iteration and the result is: red blue white (in separate lines).

For a more detailed explanation as well as an explanation of all the other 8 methods please refer to my eBook:

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!

White space in JavaScript

JavaScript does not care for white space. It just ignores it. However there is one exception to the rule: when the white space is in between quotes such as a string literal. Example:

var text = "the quick
brown fox
jumps over
the lazy dog";

If you test the above script on the Chrome’s Console (CTRL SHIFT j), JavaScript will throw an error: SyntaxError: Unexpected token ILLEGAL. We need to use the backslash \ as an escape character. An escape character is one which invokes an alternative interpretation on subsequent characters in a character sequence. In this case, the backslash will tell JavaScript to ignore the subsequent line break and just make it a continuous string:

var text = "the quick \
brown fox \
jumps over \
the lazy dog";

Notice how I included the space before the backslash and not after it. We only want to escape the line return. Now, when we call variable text it outputs the the correct sentence: “the quick brown fox jumps over the lazy dog

JavaScript, how to pass in data from the stack to the heap

One way to pass data from the stack to the heap is as an argument in a function call.

How to pass in data from stack to heap
In JavaScript, primitive data from a variable can be passed into a function as a copy, via a function argument which is given to a function parameter.

The parameter acts as the interface between stack and heap. In other words, the data in the variable and the data being passed into the function becomes independent of each other.

So even though a function resides in the heap, we are not pointing or passing by reference, we are actually cloning the data to be processed by the function, which is the same as passing by value (remember? like copying data into another variable or “adding”value).

If this intermediate level JavaScript subject interests you, I go into deeper detail on my eBook: