Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 7

computer” in its broadest sense, meaning a machine that can perform arithmetic and logical

operations. It could mean either a desktop or laptop computer (PC, Mac), a computing server, or a
mobile device like a tablet or smartphone.
A computer often requires human intervention. That’s where programmers and developers come in!
They write programs that result in instructions to a computer.
• A computer program (also called an application or software) usually comprises one or more
text files containing commands in the form of code. This is why developers are also called
coders.
• A programming language is a way to give orders to a computer. It’s a bit like a human
language! Each programming language has vocabulary (keywords that each play a specific
role) and grammar (rules defining how to write programs in that language).
Long story short:
• A computer is a machine whose role is to execute quickly and flawlessly a series of actions
given to it.
• A program is a list of actions given to a computer. These actions take the form of textual
commands. All these commands form the program’s source code.
• The programmer’s task is to create programs. To accomplish this goal, he can use different
programming languages.
• Before writing code, one must think ahead and decompose the problem to be addressed in a
series of elementary operations forming an algorithm
An algorithm is a process or set of rules to be followed in calculations or other problem-solving
operations, especially by a computer.
he main properties of an algorithm which we will now consider:
1.Input
An algorithm must possess 0 or more well-defined inputs supplied externally to the algorithm.
2. Output
An algorithm should have 1 or more well-defined outputs as desired.
3. Correctness
Every step of the algorithm must generate a correct output.
4. Definiteness
Algorithms should be clear and unambiguous, thus every step of the algorithm should be clear and
well defined.
5. Finiteness
The algorithm should have a finite number of steps that must be carried out to achieve the task at
hand.
every single step of the algorithm has to generate a correct output as long as the main output is
correct.

JavaScript is the most widely used scripting language on Earth. It has the largest library ecosystem
of any programming language.
JavaScript is the core language of the web, and the only programming language that can run in all
major web browsers.
The programs in JS are called scripts. They can be written right in a web page’s HTML and run
automatically as the page loads.
do everything related to webpage manipulation, interaction with the user, and the webserver.
For instance, in-browser JavaScript is able to:
• Add new HTML to the page, change the existing content, modify styles.
• React to user actions, run on mouse clicks, pointer movements, key presses.
• Send requests over the network to remote servers, download and upload files (so-called
AJAX and COMET technologies).
• Get and set cookies, ask questions to the visitor, show messages.
ECMA6 This version brings a lot of interesting novelties to the table. It is now well supported by
most environments and platforms, starting with web browsers
When JavaScript was created, it initially had another name: "LiveScript". But Java was very
popular at that time, so it was decided that positioning a new language as a “younger brother”
of Java would help. But as it evolved, JavaScript became a fully independent language with its own
specification called ECMAScript ,and now it has no relation to Java at all.
There are several methods to make JavaScript output and/or grab data (from the user). They either
modify or replace existing HTML, help to debug, or retrieve HTML content.
Alert() prompt() confirm()
alert() displays an alert box.prompt() stores the input from a user in a variable. confirm() returns
true if the user clicks on Ok, and false if he clicks on Cancel
You should use the console.log() method to print to console your JavaScript. The
JavaScript console log function is mainly used for code debugging as it makes the JavaScript
print the output to the console.
JavaScript, there are six primitive types:
1. boolean (true or false)
2. number (including integers like 1, -2, and floating point numbers like 3.14, 2e-3)
3. string ( Strings are used for storing text. Strings must be inside of either double or single
quotes.)
4. null (null has one value: null. It is explicitly nothing.)
5. undefined (A variable that has no value is undefined)
6. Symbol (don’t worry about them yet)
console.log(typeof(true)) // prints boolean

Composite or Non Primitive Type, like the Object we just saw.


Objects can be: “Object literals”, “Arrays”, “Function”, “RegEx”, “Dates”...
A JavaScript object literal is a comma-separated list of key-value pairs wrapped in curly
braces. These values can reference any type of data, including objects and/or primitive values.
We’ll be covering objects (& arrays) in much more details later on in this course, for now
simply keep in mind that Object literals can encapsulate a multitude of data, enclosing it in a
tidy package, and having the structure shown in the Code Box.

% operator is called modulus. The ** operator


The division 0 / 0 or using mismatching types creates a special number called not a number or
NaN.
Infinity Type:
JavaScript registers very large numbers as infinity. For instance, ten to the power of 309 is
represented as infinity. Division by zero also yields infinity.
you can’t do this => 5++, or this => 7--
Truthy & Falsy:
Before we continue let’s explore in detail, the notions of truthy and falsy.
Truthy: Something which evaluates to TRUE.
Falsy: Something which evaluates to FALSE.
It’s mostly logical. One (1) is truthy, Zero (0) is falsy. An object of any kind (including
functions, arrays, RegExp objects, etc.) is always truthy. The easiest way to determine if
something is truthy is to determine that it’s not falsy.
There are only six falsy values in JavaScript:
undefined, null, NaN(Not A Number), 0, ""(empty string), and false, of course.
Pour det la val boolenne
console.log(Boolean(undefined));

var x;
console.log(Boolean(x)); //false
var x = 5;
var y = "5";
console.log(x != y); //false

the logical operators AND and OR in JavaScript, they do not return true or false!

Returns the first value if falsy otherwise returns


and a && b the second value whatever it may be.

Returns the first value if truthy, otherwise returns


OR (||) a || b
the second value whatever it may be.
Well, we can use the “!” operator to convert a value to a boolean by negating it twice:
assuming v is truthy, !!v becomes true.
assuming w is falsy, !!w becomes false.
The && operator return the first operand if it is true or the second operand if the first is false
//false
The || operator return the first operand if it is true or the second operand if the first is false
//true
console.log(!!NaN == !!undefined && 6 >= !!Infinity) //true

The shorthand assignment operator (+=) can also be used to concatenate strings.

Template Strings:
Template Strings use backticks (`` ) rather than the single or double quotes we're used to with
regular strings.
One of their first real benefits is string substitution. Substitution allows us to take any valid
JavaScript expression (including say, the addition of variables) and insert it inside a Template
Literal using the ${} syntax, the result will be output as part of the same string.
var name = "Lucy";
console.log(`Yo, ${name}!`) // prints “Yo, Lucy!”
A function is basically a piece of code which can be reused
without having to write it again and again.
Think of a function like a mathematical function giving you a
relationship between input and output variables. If you don’t like
maths, think of a function like a vending machine. You give it
some coins and a number, and it spits out some cold soda.

The function definition example in the code box describes the relationship between its input
variables a and b, and the return value of the function.
The return statement returns the value of the function. When calling the add function with
arguments a and b, it computes the value a+b and returns it.

User-Defined Functions
Function declarations:
In most programming and scripting languages, there are some built-in functions that are kept in a
library. The real interesting part though, is that we can write our own functions, (also called 'User
Defined Functions' or 'UDF'.) to perform specialized tasks.
Before using a function, we must first define it somewhere in the scope from which we wish to call
it. Defining your own function in JavaScript is a simple task.
A classical function declaration begins with the keyword function, followed by:

• The name of the newly created function


• A list of parameters the function accepts, enclosed in parentheses and separated by commas.
• A block of JavaScript code enclosed in curly braces, { }, to be executed when the function is
called.
• The opening curly brace ({) indicates the beginning of the function code and the closing
curly brace (}) marks the termination of a function.
function functionName(param1,param2..paramN){
// block of JavaScript code
}

Function expressions: (Making functions using a variable as a reference)


We can also define functions without placing the name between the function keyword and the
argument list. This structure is great if you want to create a reference to it using a variable.
/!\ Remember, substract is still a variable, it’s just that in this case it contains a function.
var subtract = function( a, b ) {
return a - b;
}
The critical difference between function declarations and function expressions lies in how the
browser loads them into the
execution
context.
Function declarations
load before any code is executed.
Function expressions
load only when the interpreter reaches that line of code. So if you try to call a function
expression
before it's loaded, you'll get an error! If you call a function declaration instead, it'll always work,
because no code can be called until all declarations
are loaded.

function Parameters
Parameters are variables listed as a part of the function definition.
Arguments are values passed to the function when it is invoked.
In JavaScript, parameters of functions default to undefined. Default function parameters allow
named parameters to be initialized with default values if no value or undefined is passed.
var add = function( a, b ) {
return a + b;
}

console.log(add(0,1)) // prints 1
console.log(add()) // prints NaN

var add = function( a = 0, b = 1 ) {


return a + b;
}

console.log(add())
/* prints 1 even though no arguments were given to this function call */

Function parameters are the

names
listed in the function
definition
Function
arguments
are the real
values
passed to (and received by) the function at the
execution
“Calling”, “Invoking” or “Executing” are terms used
interchangeably when executing a predefined function. Functions
can be called using their references (((≃ their names))). And
necessary arguments are passed to the function during this phase
(see code box).
Note: When a function does not return anything, its return value becomes undefined

const greet = name => `Hello ${name.toUpperCase()}`

let greet = name => "Hello" + name.toUpperCase()

Every declared function has a return statement even if it is not written

here are three types of control structures:


1.Sequence: writing instructions one after the other.
2.Selection: either execute one set of instructions, or another.
3.Iteration: execute a set of instructions a finite or infinite number of times.

A for loop's header can be all blank like for (;;)


he for… in enumerates all the available indices of the values present in the array in ascending
order.
• Why bother using a variable for iteration at all, if we could enumerate the values of the
array?.
In ES6, there is a loop called the for…of which does exactly that.
var numbers = [19, 65, 1, 2, 6, 1, 9, 9, 2, 1];
var sum = 0;

for ( var i in numbers) {


sum += numbers[i];
}

// Using the for… of


for ( var value of numbers) {
sum += value;
}

break exits the closest loop it is in, and continues with the next
command.

• continue skips out from the loop body, and jumps back to the condition of the loop.
Whenever i is even, continue moves execution back to the next iteration of i in
numbers.

• .push adds elements to the end of the array.


• .unshift adds elements to the beginning of the array.
• .pop removes the last element from the array and returns it.
• .shift removes the first element from the array and returns it

We can delete any element from the array. The value undefined will be placed in place of this
element.
arr.indexOf(0) and arr.indexOf(1) return respectively, if arr holds the array [1, 5, 8]?
-1 and 0

wo ways to construct an object in JavaScript:


• The object literal, which uses curly brackets: {}
• The object constructor, which uses the new keyword
Both of these approaches will create an empty object. Using object literals is the more
common and preferred method, as it has less potential for inconsistencies and unexpected
results.
// Initializing an object literal with curly brackets
var objectLiteral = {};

// Initializing an object constructor with new Object


var objectConstructor = new Object();

Objects can have properties and methods:


• A property is the association between a name (key) and value within an object, and it can
contain any datatype. A property generally refers to the characteristic of an object.
• A method is a function that is the value of an object property, and therefore a task that an
object can perform
• An object method is a function that object can perform

/ Adding a new fight method to gimli


gimli.fight = function() {
return `Gimli attacks with an ${this.weapon}.`;
}

delete gimli.weapon; // Output: true


// We can test the output of gimli to see if it succeeded.

You might also like