JavaScript

DISCOVER JS

DISCOVER JS

  1. Declaring variables
  2. Primitive Types
  3. Declaring constants
  4. Simple manipulations and operations
  5. Conditions & Loops
  6. Arrays
  7. Functions
  8. Objects
  9. More Arrays
  10. Lambda

1. Declaring variables

// -Declarations
// var X
var name;
var anotherName;
var Name3;
var 4thName; // wrong declaration
var diffrent_name;
var §erfez; // wrong declaration
var $name;
var _name;

2. Primitive Types

// Types
var
// Numbers
	num = 5,
	float = 6.3,
// strings
	text = "message",
	text2 = 'message2',
// bool
	conditionT = true,
	conditionF = false,
// undefined and null
	empty1= null,
	empty2= undefined;

3. Declaring Constants

// Constants
const Trainer= "Youcef Madadi";
const Students= 30;

Trainer = "Someone Else" /* Uncaught TypeError: Assignment
                                * to constant variable.*/

const Assistant; /* Uncaught SyntaxError: Missing initializer 
                  * in const declaration*/

4. Simple manipulations and operations

// Arithmetic operations
x = 5 + 6; // addition
y = 7 - 3; // minus
z = 4 * 4; // multiplication
a = 7 / 2; // division
b = 4 ** 2; // power
b = b % 2; // Modulus
a++; // Increment
b--; // Decrement

Arithmetic operations

4. Simple manipulations and operations

// String manipulation
name = "Youcef";
text = "Hello new user : " + name;
message= `From ${name} : ${text}`

String manipulation

4. Simple manipulations and operations

// Logical operations
c = (x >= y); // ( x > y )
d = (y <= z); // ( y < z )
e = (z == x);
f = (5 == "5");
g = (5 === "5");
h = (5 !== "5");
i = (name == text);
j = (!g);
k = (c && e);
l = (c || e);

Logical operations

4. Simple manipulations and operations

// Binary operations
m = (7 & 6);
n = (10 | 9);
o = (~z);
p = (x ^ z);
q = (5 >> 1);

Binary operations

5. Conditions & Loops

// Conditions
if (/* condition1 */) {
  /* Instractions 1 */
} else if (/* condition2 */) { // optional
  /* Instractions 2 */
} else { // optional
  /* Instractions 3 */
}

Conditions

// question mark operation (ternary) 
// ( condition ) ? (true Result) : (false Result)
var max = a>b ? a : b;

Conditional (ternary) operator

5. Conditions & Loops

// Switch 
switch(Bounadem){
  case Trainer:
    console.log("You can Teach go on");
    break;
  case Assistant:
    console.log("Well he gotta take care of the trainer");
    break;
  case CoAssistant:
    console.log("You can just sell della3")
    break;
  default:
    console.log("You are a trainee");
    break;
}

Switch case

5. Conditions & Loops

// loop 1
while (/* condition */) {
  /* Instractions to repeat */
} 

Loops (while)

// loop 2
do{
  /* Instractions to repeat */
} while  (/* condition */) ;

Loops (do while)

5. Conditions & Loops

// loop 3
for (var i = 0; i < N; i++) {
  /* Instractions to repeat */
}

Loops (for)

// loop 4
for (var i = N; i > 0; i--) {
  /* Instractions to repeat */
}

Loops (for reverse)

6. Arrays

// Array Declarations
var list=[];
var Notes=[12,14,8,16,4,9,13,12,"Absent"];

Declaration

// Array Assignment
list[0]="Youcef";
list[4]="Abdelhak";

Assignment

7. Functions

// Declaring a function
function Max(a, b) {
  if (a > b) return a;
  else return b;
}

Declaring a function

// Declaring a function in a variable
var a=prompt("enter a"),
    b=prompt("enter b");
console.log( Max( a , b ) );

Calling a function ( invoking )

7. Functions

// Declaring a function in a variable
var maxf = function(a,b){
	if(a>b) return a;
	else return b;
}

Declaring a function in a variable

// name of a function
console.log( Max.name , maxf.name );

Name of a function

7. Functions

// Sending function as a parameter
function call(Caller,a,b){
	return Caller(a,b);
}

Sending a function as a parameter

// not setting any arguments and getting away with it
function Max(){
	var max=-Infinity;
	for(var i=0; i < arguments.length ; i++){
		max=Math.max(max,arguments[i]);
	}
	return max;
}

Arguments for the function

8. Objects

// Declaring an object
var person={
	first_name:"Youcef",
	"last name":"Madadi",
	age:23
}

Declaring an object

// Accessing data in objects
console.log(
	person.first_name,
  	person["last name"],
  	person["age"]
)

Accessing Data in an Object

8. Objects

// Getting Keys in an object
for(var key in person){
	console.log(key);
}

Get Objects properties

// Getting Keys of an object
Object.getOwnPropertyNames(person)
Object.keys(person)

Getting Keys

8. Objects

// puting functions in objects (methods)
var person1 = {
  first_name: "Youcef",
  "last name": "Madadi",
  age: 25,
  todoList:[],
  ToDo: function (Do) {
    todoList.push(Do);
    return `I'm Going to do : ${Do}`;
  },
};

Methods

8. Objects

// this key word to access other members
var person2 = {
  first_name: "Youcef",
  "last name": "Madadi",
  age: 25,
  intreduce: function () {
    console.log(
      `I'm ${this.first_name} ${this["last name"]} and i'm ${this.age} years old`
    );
  },
};

this key word

8. Objects

// Genrate Objects (classes first form)
function Person(first_name, last_name, age) {
  this.first_name = first_name;
  this.last_name = last_name;
  this.age = age;
  this.fullName = function () {
    return this.first_name + " " + this.last_name;
  };
}
var p1 = new Person("Youcef", "Madadi", 25);
var p2 = new Person("Someone", "Else", 22);

Constructing Object (new key word)

9. More Arrays

// Getting Keys in an object
Peoples=[p1,p2,p3...]
console.log(Peoples.length)

Length

10. Lambda functions

// Lambda functions
var sum = function (a, b) {
  return a + b;
};
// we could write the above example as:
sum = (a, b) => a + b;
// or
sum = (a, b) => {
  return a + b;
};

var max = (a, b) => (a > b ? a : b),
  fact = (n) => (n > 1 ? n * fact(n - 1) : 1);

Node js Chapter 1

By Youcef Madadi

Node js Chapter 1

  • 372