Blog

Learning JavaScript

Thursday, November 21st, 2019

11/21/19 Learning JavaScript has been on my task list for the past few years. Unlike other programming languages that just integrate into the software packages. JavaScript. The interesting thing about learning JavaScript is that most community, or online classes speak to you as if you already know the information. Which is sadly not helpful to pretend to teach.

So it was with GREAT JOY that I found Learn JavaScript in 1 Hour [2019]. The tutorial includes a very helpful way of working with JavaScript in the Chrome Browser. I found that working in the browser made more sense to me, than setting up a local server. I am not a local server developer. I work all of my problems out online. Setting up a local server and working on command lines is quite foreigner to me. All the other software I use (which is a very wide range) is all interface driven. In recent years, due mostly to Adobe changing so many of the fundamental shortcut keys. I am finding that I am using even the shortcut keys even less than before. And clicking around the interface for the tool needed.

JavaScript Variable

Much like other languages Variables are used to temporarily store data and follow most of the basic rules for naming conventions. 

What a Variable Can Not Be

  • No reserved keyword (that JavaScript already uses).
    • True, False, typeof, let, conts,
  • Variable are case-sensitive.
  • Can not start with a number (1new).
  • Can not have a space or hyphen.

Best Practices for Variables

  • Declare each variable on a single line.
  • Use camelNotation for variable name firstName / lastName
  • Variable names should be relevant.

How to declare and initialize a Variable

Variable Name (Variable Names are unique labels for data containers).
Container is the Variable Value.

Use Let to declare a variable.
let – create a variable (we no longer use var)

What values can be assigned to a Variable?

There are the two types of variables.

  • Primitives / Value Types 
    • String
      • let name = ‘Gary’; //string literal
    • Number
      • let age = 47; //number literal
    • Boolean
      • let isApproved = true; // boolean literal true or false
    • Undefined
      • let firstName = undefined; //not very common if not initialzed it will be undefined
    • Null
      • let lastName = null; //to clear the value of a variable
    • Symbol
  • Reference Types 
    • Objects (properties – multiple related variables)
    • Array
    • Function

JavaScript Constants

At times, when the variable needs to remain the same value. It is best to use a Constant instead of a variable. The value of a Constant can not change.

const – create a constant

Dynamic Typing

JavaScript is a Dynamic Language. With a dynamic language the type of variable can change at run-time.

JavaScript Objects

let person = {
    name: ‘Gary’,
    age: 47
};
//Dot Notation
person.name = ‘John’;
//Bracket Notation
person[‘name’] =’Mary’;
console.log(person.name);

Arrays

Arrays is where all my other tutorials have fallen apart. All the other aspects of Variables (up until this point I understand from years of coding with ActionScript). I used arrays in ActionScript, but the information has not aligned. So I am excited to see how I manage with Mosh’s explanation.

An Array is a data structure that is used to represent a list of items. 

Arrays are used to store a list of items.

[ ] = Array Literal and is an empty array. Initialize the array by adding items.

let selectedColors = [‘red’, ‘blue’];
console.log(selectedColors);
let selectedColors = [‘red’, ‘blue’];
selectedColors[2] = ‘green’;
console.log(selectedColors);

The theory of arrays I have. The examples used in the tutorial worked. The follow code – has the variable change from a string to a number (which is cool). I do feel that I am getting arrays slightly better.

let selectedColors = [‘red’, ‘blue’];
selectedColors[2] = 1;
console.log(selectedColors.length);

Functions

The basic building block of JavaScript.

A function is a set of statements that:

  • Calculates and returns a value.
  • Or performs a task.

Declares function in browser. This is a statement that is terminated by the semicolon. When declaring a Function you do not need a semicolon as it is not a Variable.

function greet(){
console.log (‘Hello’);
}
greet();
Expand the Function by adding a Variable (parameter). This function has one variable that is only meaningful in the function. And will not be accessible outside this function.
function greet(name){
console.log (‘Hello’)
}
greet();
Add an Argument to the Call. (‘Gary’)
(name) is a parameter of the greet function.
Understand the difference between a Parameter and an Argument.
Parameter is the variable at the time of declaration.
Argument is the actual value we supply for that parameter.
function greet(name){
console.log (‘Hello’ + name)
}
greet(‘Gary’);
Call two values to the browser
function greet(name){
console.log (‘Hello ‘ + name)
}
greet(‘Gary’);
greet(‘Fella’);
Function can have multiple parameters.
// Performing a task
function greet(name, lastName){
console.log (‘Hello ‘ + name + ‘ ‘+ lastName)
}
greet(‘Gary’, ‘Smith’);
Function that calculates a value.
Return is called the function.
// Performing a task
function greet(name, lastName){
console.log (‘Hello ‘ + name + ‘ ‘+ lastName)
}
// Calculaing a value
function square(number) {
return number * number;
}
let number = square(2);
console.log(number);
The variable can be moved to the console log for the same outcome.
// Performing a task
function greet(name, lastName){
console.log (‘Hello ‘ + name + ‘ ‘+ lastName)
}
// Calculaing a value
function square(number) {
return number * number;
}
console.log(square(2));

Leave a Comment