Skip to content

Instantly share code, notes, and snippets.

View jacqueslandrieu's full-sized avatar

Jacques Landrieu jacqueslandrieu

View GitHub Profile
/*
FUNCTIONS: PROGRAMS WITHIN PROGRAMS!
There are two phases to using functions. First we must declare (define) it.
We declare a function and name it square. We could put parameters in paranthesis "()":
*/
function square(number) {}
/*
Now we can invoke/call/execute the function. It happens between curly brackets "{}".
/*
LOOPS: WHILE, FOR, FOR-IN
While loops are less popular than for loops.
They are more manual and work better with numbers.
*/
var count = 0;
while (count < 11) {
count++; // if you will miss this part, program will crash
} // prints 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. It stopped at 11.
/*
CONTROL FLOW: IF, ELSE-IF, ELSE
Conditionals are great to use with booleans.
*/
var a = 1;
if(a > 0) {
print('Yes');
} if (a === 1) {
/*
STRING MANIPULATION
The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string.
Search() works the same way:
*/
var str = 'Find where the string starts';
var res = str.indexOf('s');
console.log(res); // prints 15, it means first 's' is at 15th character
/*OPERATORS
With special operators we could do many tasks, like assigning, comparing etc.
ASSIGNMENT. Use the "=" sign to assign values to variables:
*/
x = 1; // value of 1 is assigned to x
x += 1; // means x = x + 1
/*
DATA TYPES (Simple & Complex)
JavaScript has built-in data types, which are called simple or complex.
*Simple Data Types
Simple data types are data types whose values cannot be changed.
The simple data types are
/*
* VARIABLES:
*
* Variables essentially function as placeholders for data/values. They can hold the place of things as simple
* as a number, a string (e.g. var name = "Jacques") or Boolean to Arrays, Objects, or other data-types.
* Variables... vary. Or at least they can if you want. You can change the value of a variable from a number
* to a number, a number to a function, an oject to a function, etc.
*HOW TO CREATE A VARIABLE?