The Full JavaScript & ES6 Course Notes : JavaScript Language and Essentials

20181014
JavaScript Language and Essentials
-Syntax, statements, data types, variables, and assignment
-Functions, scope, arrays, and objects (Next article)
-JS Conditions and Loops

Syntax, statements, data types, variables, and assignment

Syntax : Syntax refers to the set of rules that define how we construct programs.

Statement : Now our program runs of a series of instructions for javascript execute. We call these instructions “Statement”. (Consist of values expressions keywords and commands.) *keywords are something like var, if else.

Data types : Data inside quotation marks are strings of characters that simply represent words that could either meaning something in English or even something random.The single quotation mark can be interchange with the double quotation mark and still create a valid string.

var a = b

a = ‘hello‘(or “hello” )

b = a + ‘hello’

Install live-server

Comment:

  1. //

2. /*

*/

Rules that naming var identifiers in javascript :

1. We can’t really use reserve javascript keywords for our variable names.

2.Our variable names cannot start with a number.

(o) var $_a = 30;

(x) var 1a = 30;

3.Javascript keywords or variable names or identifiers cannot have spaces within them.

(o) var first_name = 30; // snakes

(o) var fristName = 30; // camels

(x) var first name = 30;

Operaters

Javascript includes a series of operators that we can employ in order to calculate numbers and even manipulate strings. (Including multiplication and division.)

var z = 10 + 4–2;

console.log(z);

As a result we see 12 in our console.

Using multiplication and division :

In these two examples since we use an operator we can also call them expressions.

Concatenation(Use the plus sign on strings to join them together)

var a = “Good” + “day”;

console.log(a);

As a result we see Good day in our console.

Division(it returns the remainder of the operation rather than the quotient or result of the operation.)

var b = 11 % 3;

console.log(b);

As a result we see 2 in our console. // 11%3 = 3… remain 2

var b = 9 % 3;

console.log(b);

As a result we see 0 in our console. // 9%3 = 1… remain 0

Functions, scope, arrays, and objects

Function : A function in javascript defined a block of code that performs a specific task.

A function executes when our program calls it in a statement or expression. In fact console.log is a function. Let’s examine how to declare a function.

Now using the javascript syntax. Using keyword function + name and we specify the number of arguments for a function with a pair of parentheses immediately following our function name. We specify the body of the function with a block of code represented by a pair of curly braces.

function print(){

}

and then console.log Now before we see any output we’ll have to invoke the function with the call to it.

function print(){

console.log(“A working function!”);

}

Let’s add that statement and all we have to do to call the function is specify its name with the parentheses and save.

function print(){

console.log(“A working function!”);

}

print();

Let’s add a variable A with the print and put a in console.log .

function print(a){

console.log(a);

}

Pass in a statement to the print function.

function print(a){

console.log(a);

}

print(“An argument!”);

Functions can have as many parameters as we design.

function print(a, b, c){

console.log(a + b + c);

}

print(“One”, “Two”, “Three”);

Functions can even be assigned to variables.

var print = function(a, b, c){

console.log(a + b + c);

}

print(“One”, “Two”, “Three”);

Functions that return data

  • Declare a new function called multiply it will have two parameters a and b.

var multiply = function(a, b);{

}

  • And this time this function will return a times b.

var multiply = function(a, b);{

  • return a*b;

}

Now this function won’t just console.log data but will actually send back data when you invoke the function.

  • So now we can create another variable called product and set it to the result of calling multiply on two numbers which will be 5 in 10.

var multiply = function(a, b);{

  • return a*b;

}

var product = multiply(5, 10);

  • And now let’s console.log product

var multiply = function(a, b);{

  • return a*b;

}

var product = multiply(5, 10);

console.log(product);

We see 50 inner.

Notice : the product was assigned to the returned value of the two numbers we pass in our multiply function.

Object :

Let’s talk about this by considering a dog. dogs in real life have a name and breed. So let’s talk about how we can map or model this specific dog in a javascript object.

If we map a dog into Javascript objects, the concept will look like this :

var dog = {
name : ‘Buddy’,
breed: ‘Golden Retriever’,
weight: 60
};

console.log(dog);

*every object need to using comma until reach the last one.

we could also console.log any object we want(using . or string):

var dog = {
name : ‘Buddy’,
breed: ‘Golden Retriever’,
weight: 60
};

console.log(dog.breed); or console.log(dog[“breed”]);

We can even add methods or functions to this dog object.

Now let’s add a bark and map it to a function which just console.log.

And we can call a dog.bark to invoke that bark function and then we could see “Woof!” in our console.

var dog = {
name : ‘Buddy’,
breed: ‘Golden Retriever’,
weight: 60
bark: function( ){
console.log( “Woof!”);
}
};

dog.bark( );