JavaScript First Steps

The last of my JavaScript trilogy writings is out:

Web Literacy, Programming for Everyone – JavaScript First Steps, introduces essential concepts for the understanding of programming by using the most popular language on the web: JavaScript.

This booklet is part of a series covering specific levels of digital/web literacy based on HTML and related technologies. Each one is somewhat independent of the other in the sense that a reader may approach one subject without reading another previously published.

The book was kept short on purpose and it represents a level of knowledge covering essential techniques the reader may use forever.

Link to Amazon ebook
eBook Web Literacy Programming for Everyone JavaScript First Steps

Who Should read this book?

This booklet has been written for those who really want to learn programming as part of their general education. The author assumes the reader has no prior knowledge on the subject. The only prerequisite is an open mind and willingness to try something new.

Please check it out and let me know if it helped you.

Use the free Kindle app to read this and other eBooks on your computer.

Advertisements

JavaScript For Loop How does it work?

What does this code mean?

for (var i = 5; i < 51; i++) {
console.log(i);
}

for_loop

Imagine a musical metronome with a big arm swinging left and right.

Imagine that each time the arm clicks left, right, left, you say out loud 5, then 6, then 7, etc.

Ok, in the script above, the ticking of the metronome is a temporary variable called i.

You are JavaScript. And your voice out loud is console.log( ).

The script above takes an arbitrarily named variable, i that keeps pointing at numbers, starting at 5 and stopping at 50 (inclusively). That’s the ticktock of the metronome.

JavaScript guides itself by the “sound” of i, and it performs the command inside of the body of the loop which is to display out loud “5”, “6”, “7”… “50”. On 50 i stops counting and JavaScript knows it is time to exit the loop.

Console.log() prints out each number at each position of i.
i++ is a shortcut that means “increment by 1”, which happens once on each cycle of the loop.

for i starting at 5, i less than 50, increment i
while incrementing, print each time, the value "at" i

Using JavaScript to draw six random numbers

I’ve got a new booklet on Amazon titled “Drawing Six ‘Lucky’ Numbers”.
This is the first of a series called “Small Projects to Learn JavaScript”.

The idea is to offer micro projects where the reader practices and develops awareness of practical JavaScript concepts.

small projects to learn javascript
On this first booklet the project goes under the following rules
• To fetch five random numbers ranging from 1 to 75
• To fetch a sixth random number ranging from 1 to 15.
• The first five numbers cannot be repeated.
• Zero is not allowed.
• Numbers should be displayed in numerical order from the lowest to the highest, except for the sixth number, which stands on its own at the extreme right.Easy enough, right?If it sounds like a lottery drawing, it is because the rules for drawing numbers are identical to a known national game.

Hey, perhaps you will get lucky with it since random numbers are random numbers!
Don’t forget about me if you ever make it!

The booklet covers fundamental concepts such as:

•    Searching numeric arrays.
•    Using For loops.
•    Isolating code with functions.
•    Usage of Math random, ceil, floor and round.
•    How to separate code by its functionality.
•    A practical application of indexOf
•    Practicing using a Console.
•    Displaying an Array as string

Give it a try, it’s only 99 cents in the US, less than a cup of joe!

If you’re interested follow this link or click on the image:

http://www.amazon.com/Small-Project-JavaScript-Lucky-Numbers-ebook/dp/B00H4P987A/

JavaScript declarations best practice

JavaScript converts all variable declarations into a single var, separating variables by commas.  It is good practice to do the same as we code, in order to speed up process, and most importantly, to avoid being misinterpreted.

// Single variable declaration:
var x = 10;
// multiple variables declaration:
var y = 5,
	z = 7,
	a = "Hi there"
;
// above, notice the semicolon at the end (and no comma after the last item)
// And in loops:
var i;
for (i = 0; i < 5; i++)
// be aware that unless a loop is inside of a function, 
//i will remain global after the loop is over, holding the very last value.

It is best to declare all variables on the top of their scopes. In Function scope they can also be grouped with one single var.

// variable declaration inside of functions
function xyz(num){
var c = 10,
    d = 2
	;
return c * num / d;
};

JavaScript makes two passes to the code. The first pass is called hoisting.

When the interpreter scans the code it makes one first pass and gathers variable declarations and function names making a menu of what is available. At this point JavaScript does not yet know what is in each hoisted variable (pulled to top) and they all have the same value: undefined.

On the second pass that’s when JavaScript finally gathers the value of each variable making up an execution environment or execution context, good only for that moment in time. This execution context is never the same since JavaScript is like water in a river, it never looks back. It may push some variables forward but each execution context is unique.

You can read more about those issues on my latest book.

If you love JavaScript like I do, you will enjoy reading it.
Use the free amazon “Kindle for PC” app to read it on your computer.