Dividing the code into functions is essential.
Basically, a function can take an input (from zero to several parameters - a parameter can be a function) and provide a result whenever it's called.
In JavaScript, a function is a value.
A declared function is visible in the whole script.
function sayHello() {
console.log("hello")
}
The function name is usually a verb briefly describing the action. One function should do one action. Here's how to call it:
sayHello() // consols "hello"
When reached, the return keyword makes the function stop and returns the results if any (default return value is undefined)
function checkAge(age) { return age >= 18 ? true : false;}
checkAge(18) // returns true
checkAge(10) // returns false
A function can update variables without returning anything. For instance (from FreeCodeCamp) :
let sum = 0;
function addSum(num) {
sum = sum + num;
}
addSum(3); // sum variable now equals 3
console.log(addSum(3)) // returns undefined
Consider this function expecting three parameters:
function add(a, b, c) {
return a + b + c
}
If a parameter is not passed on, it's value is considered as undefined.
add(4, 2) // returns NaN (Not a Number)
You can specify a default value if the parameter is omitted:
function addition(a, b, c = 0) {
return a + b + c
}
add(4, 2) // returns 6
The rest parameter (...param) is useful if we want a function to work with a variable number of arguments. Arguments passed this way are stored as an array. Check out this example:
function howMany(...args) {
return args.length;
}
console.log(howMany(0, 1, 2)); // returns 3
console.log(howMany("string", null, [1, 2, 3], { })); // returns 4
If parameter order does not matter, you can use a JSON object as a parameter:
function myFunction(params) {
// do something
}
myFunction( { prop1: value1, prop2: value2 } );
Anonymous or expressed functions are useful when we don't need to re-use them anywhere else in the code. It exists once it's reached. Here are two use-cases with possible syntax:
Examples from FreeCodeCamp:
const myFunc = function() {
const myVar = "value";
return myVar;
}
With ES6 it can be shortened into an arrow function:
const myFunc = () => {
const myVar = "value";
return myVar;
}
Note that if we only want a value to be returned, then "return" keyword as well as brackets surrounding the code can be omitted.
const myFunc = () => "value";
If we need to pass arguments, the same principle as for named functions applies, except than parenthesis enclosing the parameter can be omitted if there's only one parameter:
const doubler = item => item * 2;
doubler(4);
const multiplier = (item, multi) => item * multi;
multiplier(4, 2);
Consider this object literal declaration (from FreeCodeCamp) which returns an object containing two properties:
const getMousePosition = (x, y) => ({
x: x,
y: y
});
Since ES6 a syntactic sugar exists. It eliminates the redundancy:
const getMousePosition = (x, y) => ({ x, y });
Here, x will be converted to x: x.
Write the function into parenthesis and call it at the end with parenthesis as well. This is call a IIFE notation.
(function(a) {
console.log(a);
})("hey");
These past few weeks I learned about regular expressions. Here's a recap! Almost all examples come from FreeCodeCamp.
let stringToTest = "a string";
let regEx = /pattern/;
regEx.test(stringToTest); // returns false
returns true or false depending if the pattern is found or not
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex); // returns ["expressions"]
returns an extraction of the string that matches the provided pattern.
let wrongText = "The sky is silver.";
let silverRegex = /silver/;
wrongText.replace(silverRegex, "blue"); // returns "The sky is blue"
searches for the described pattern and replacing it by a value of your choice.
You can also access capture groups in the replacement string with dollar signs ($) (see group capture in Patterns/Group elements/Reuse group).
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1'); // returns "Camp Code"
This is actually not a method but a special pattern to look ahead in the string for patterns further along. It won't match the element.
Use (?=...) to verify the element ("...") is there
Use (?!=...) to verify the element ("...") is not there
let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex); // returns ["q"] ("u" is there)
noquit.match(qRegex); // returns ["q"] ("u" is not there)
This is not a method either but note that regular expressions are by default greedy meaning they will find the longest possible part of a string that fits the regex pattern and returns it as a match.
let string = "titanic";
let regEx = /t[a-z]*i/;
string.match(regEx); // returns "titani"
You can use ? to change it to lazy matching meaning the regular expression will find the shortest possible part of the string.
let string = "titanic";
let regEx = /t[a-z]*?i/;
string.match(regEx); // returns "ti"
Append flag right after the end of a pattern (//) if you need to extend the search to the whole string and/or ignore letter case.
Use g flag to search or extract a pattern more than once.
let testStr = "Repeat, Repeat, Repeat";
let repeatRegex = /Repeat/g;
testStr.match(ourRegex); // returns ["Repeat", "Repeat", "Repeat"]
Use i flag to ignore letter case (ie the difference between uppercase and lowercase letters).
let repeatRegex = /ignorecase/i;
This regex can match the strings ignorecase, igNoreCase, and IgnoreCase.
Just concatenate them, for instance
let regEx = /search/gi
Define a character set between square brackets []
Inside, define a list of character or a range of characters, using a hyphen character - to seperate from and to elements. Letters and numbers can be combined.
let regEx1 = /aeiu/
let regEx2 = /[a-e]/
let regEx3 = /[3-9]/
let regEx4 = /[a-e3-9]/
let catStr = "cat";
let batStr = "bat";
let matStr = "mat";
let bgRegex = /[a-e]at/;
catStr.match(bgRegex); // returns ["cat"]
batStr.match(bgRegex); // returns ["bat"]
matStr.match(bgRegex); // returns null
Add ^ at the beginning of the character set you want to negate
let regEx = /[^aeiou]/gi // matches everything that is not a vowel
Use () to group elements
let testStr = "Pumpkin";
let testRegex = /P(engu|umpk)in/;
testRegex.test(testStr); // Returns true
A group is saved as a temporary "variable" that can be accessed within the same regex using a backlash and the number of the captured group (e.g. \1). The number corresponds to the position of their opening parentheses, starting at 1.
let repeatStr = "row row row your boat";
let repeatRegex = /(\w+) \1 \1/;
repeatRegex.test(repeatStr); // Returns true
repeatStr.match(repeatRegex); // Returns ["row row row", "row"]
Use ? right after the element
let american = "color";
let british = "colour";
let rainbowRegex= /colou?r/;
rainbowRegex.test(american); // Returns true
rainbowRegex.test(british); // Returns true
Use | (OR operator) between each element
let regEx = /yes|no/
Use wildcard character . to match any one character
let humStr = "I'll hum a song";
let hugStr = "Bear hug";
let huRegex = /hu./;
huRegex.test(humStr); // returns true
huRegex.test(hugStr); // returns true
Type the string you're looking for between / and /
let testStr = "Hello, my name is Kevin.";
let testRegex = /Kevin/;
testRegex.test(testStr); // returns true
Use \w
let longHand = /[A-Za-z0-9_]+/;
let shortHand = /\w+/;
For everything but letters, numbers and underscore, use \W (short hand for [^A-Za-z0-9_])
Use \d
let longHand = /[0-9]/;
let shortHand = /\d/;
For all non-numbers, use \D (short hand for [^0-9])
Use \s to look for whitespace, carriage return (\r), tab (\t), form feed (\f), new line (\n) and vertical tab (\v)
let longHand = /[\r\t\f\n\v]/;
let shortHand = /\s/;
For non-whitespace, use \S (shorthand for [^ \r\t\f\n\v])
Use ^ outside of a character set and at the beginning of the regex
let firstString = "Ricky is first and can be found.";
let notFirst = "You can't find Ricky now.";
let firstRegex = /^Ricky/;
firstRegex.test(firstString); // returns true
firstRegex.test(notFirst); // returns false
Use $ at the end of the regex
let theEnding = "This is a never ending story";
let storyRegex = /story$/;
storyRegex.test(theEnding); // returns true
let noEnding = "Sometimes a story will have to end";
storyRegex.test(noEnding); // returns false
Use + right after the character than can be found one or more times
let string1 = "abc"
let string2 = "aabc"
let string3 = "abab"
let regEx = /a+/g
string1.match(regEx); // returns [ 'a' ]
string2.match(regEx); // returns [ 'aa' ]
string3.match(regEx); // returns [ 'a', 'a' ]
Use * right after the character that can be found zero or more times
let soccerWord = "gooooooooal!";
let gPhrase = "gut feeling";
let oPhrase = "over the moon";
let goRegex = /go*/;
soccerWord.match(goRegex); // returns ["goooooooo"]
gPhrase.match(goRegex); // returns ["g"]
oPhrase.match(goRegex); // returns null
Specify a certain number of patterns using curly bracket {}
let A4 = "haaaah";
let A3 = "haaah";
let A100 = "h" + "a".repeat(100) + "h";
let multipleHA = /ha{3}h/;
multipleHA.test(A4); // returns false
multipleHA.test(A3); // returns true
multipleHA.test(A100); // returns false
To only specify the lower number of patterns, keep the first number followed by a comma.
let A4 = "haaaah";
let A2 = "haah";
let A100 = "h" + "a".repeat(100) + "h";
let multipleA = /ha{3,}h/;
multipleA.test(A4); // returns true
multipleA.test(A2); // returns false
multipleA.test(A100); // returns true
Put two numbers between the curly brackets, separated by a comma - for the lower and upper number of patterns.
let A4 = "aaaah";
let A2 = "aah";
let multipleA = /a{3,5}h/;
multipleA.test(A4); // returns true
multipleA.test(A2); // returns false
Let's continue our exploration of data sets in JavaScript by having a look at the objects!
An object is also a collection of data but in a structured way as it's stored by pairs of property (or key) / value. For instance:
const BOOK = {
title: "The Lord of the Rings",
"volumes": 3,
"main characters": ["Frodo", "Aragorn"],
9: ["Fellows", "Nazgul"]
};
Notes:
To access the propertie's value, you can either use the dot or the bracket notation:
const TITLE = BOOK.title;
const VOLUMES = BOOK["volumes"];
const MAIN_CHARACTERS = BOOK["main characters"];
const NINE = BOOK[9]
You can use the dot notation if your property in a string of a single word. Otherwise, user the bracket notation with quotes for strings and without for numbers.
Tip: not sure a property exists? Use optionnal chaining (?.)! It helps prevent errors when accessing nested properties that might be null or undefined.
console.log(BOOK?.isbn);
Properties stored as value of a variable can also be access with the bracket notation. For instance:
const MAIN_CHARACTERS = "main characters";
let charactersList = BOOK[MAIN_CHARACTERS];
console.log(charactersList);
will print
[ 'Frodo', 'Aragorn' ]
on the console.
This can be very useful for iterating through an object's properties.
The same principle applies to access to sub propertie's values, just continue to filter via bracket or dot notation until you reach your destination. For instance (from FreeCodeCamp):
const ourStorage = {
"desk": {
"drawer": "stapler"
},
"cabinet": {
"top drawer": {
"folder1": "a file",
"folder2": "secrets"
},
"bottom drawer": "soda"
}
};
ourStorage.cabinet["top drawer"].folder2; // returns string "secrets"
ourStorage.desk.drawer; // returns string "stapler"
To check if the property of a given object exists or not, use the .hasOwnProperty(propname) method. It returns true or false if the property is found or not. For instance:
BOOK.hasOwnProperty("title");
BOOK.hasOwnProperty("author");
The first hasOwnProperty returns true, while the second returns false.
Note: always use quotes for string properties, otherwise an error is thrown.
Use Object.keys() method. It takes an object as the argument and returns an array of strings representing each property in the object. The order is first all non-negative integer keys in ascending order by value, then other string keys in ascending chronological order of property creation. For instance:
Object.keys(BOOK) // returns [ '9', 'title', 'volumes', 'main characters' ]
Same principle. Use Object.values() method.
Object.values
(BOOK) // returns [ [ 'Fellows', 'Nazgul' ], 'The Lord of the Rings', 3, [ 'Frodo', 'Aragorn' ] ]
Same principle. Use Object.entries() method.
Object.entries
(BOOK) //returns [ [ '9', [ 'Fellows', 'Nazgul' ] ], [ 'title', 'The Lord of the Rings' ], [ 'volumes', 3 ], [ 'main characters', [ 'Frodo', 'Aragorn' ] ] ]
Use a for...in statement. It looks like this:
for (let item in BOOK) {
console.log(item);
} // logs 9, title, volumes, main characters, each value in its own line
Here, "item" is a variable we define - it could be any name. Its value will change at each iteration.
The example comes from FreeCodeCamp.
Consider this object:
const user = { name: 'John Doe', age: 34 };
To extract both values and assign them to variables you could use:
const { name, age } = user;
Read it as: "create constants named "name" and "age" containing values from "name" and "age" properties in "user" object". This is a shorthand equivalent of
const name = user.name;
const age = user.age;
A console.log on name and age would return John Doe and 34, each on its own line
const declaration can by bypassed. To protect data from mutation, use Object.freeze() function. The argument would be the object you'd like to freeze. For instance (from FreeCodeCamp):
let obj = {
name:"FreeCodeCamp",
review:"Awesome"
};
Object.freeze(obj);
obj.review = "bad";
obj.newProp = "Test";
obj.review and obj.newProp assignments will result in errors and the object wouldn't change.
Proceed like any other variable; using dot or bracket notation. For instance:
BOOK["main characters"][1] = "Sam";
BOOK["main characters"][2] = "Bill";
console.log(BOOK["main characters"]);
will print
[ 'Frodo', 'Sam', 'Bill' ]
on the console.
Proceed just like you would for updating. For instance:
BOOK.format = "";
BOOK["number of copies"] = 100;
BOOK[5] = "Hello";
BOOK object will now look like this:
{
'5': 'Hello', '9': [ 'Fellows', 'Nazgul' ], title: 'The Lord of the Rings', volumes: 3, 'main characters': [ 'Frodo', 'Aragorn' ],
format: '', 'number of copies': 100
}
Order has not much importance from an object perspective because you're not using indexes to access data. But if this is important, consider using the spread operator (I'll probably write a blog entry later on this subject). Note that number properties order themselves at the beginning of the object.
User the delete keyword and then target your property according to it's format. For instance:
delete BOOK.format;
delete BOOK["number of copies"];
delete BOOK[5];
This is a personal step by step procedure to be updated.
cd Documents/www/project
git init
git add .
git commit -m "initial commit"
git remote add origin URL
git push -u origin master
cd Documents/www/project
git add .
git commit -m "changed this"
git remote add master URL
git push -u master
I learned a few things about JavaScript on OpenClassrooms (taking Apprenez à programmer avec JavaScript & Écrivez du JavaScript pour le web courses). I didn't know it then but I was actually looking for DOM manipulation for my Qwixx project. Anyway, I'm refreshing my newbie knowledge by taking FreeCodeCamp JavaScript Algorithms and Data Structures course and here is a recap about arrays.
An array can store multiple pieces of data in a single variable. Each entry is separated by a comma and nested into square brackets. Just like this:
const SANDWICH = ["peanut butter", 1, true, ["bakery", "home-made"]];
An array containing another array is also called a multi-dimensional array. An array can also contain objects.
"peanut butter" | 1 | true | ["bakery", "home-made"] |
0 | 1 | 2 | 3 |
Arrays use zero-based indexing, so the first element in an array has an index of 0. Use square bracket notation to get the data associated to the index. For instance:
console.log(SANDWICH[0]);
const EATEN = SANDWICH[1];
console.log(EATEN);
will print "peanut butter" string and 1 number.
Note that additional pair of brackets refers to the next level of entries inside.
const BOUGHT_IN = SANDWICH[3][0];
console.log(BOUGHT_IN);
will print "bakery".
Use indexOf() method. It returns -1 if the item is not found. For instance:
SANDWICH.indexOf('peanut butter'); // returns 0
SANDWICH.indexOf('butter'); // returns -1
Use spread operator (...). For instance (from FreeCodeCamp):
const arr = [6, 89, 3, 45];
const maximus = Math.max(...arr); // maximus has the value of 89
Use a for loop. It looks like this:
let array = [1, 2, 3]
for (let i = 0; i < array.length; i++) {
console.log(array[i])
} // logs 1, 2, 3, each on its own line
Even if an array is defined as a const its entries are mutable. From our example,
SANDWICH[0] = "houmus";
SANDWICH now has the value
["houmus", 1, true, ["bakery", "home-made"]];
Use push() method. It takes one or more parameters and "pushes" them onto the end of an array. For instance:
const arr = [1, 2, 3];
arr.push(4);
arr now has the value [1, 2, 3, 4]
Use unshift() method. For instance:
const arr = [1, 2, 3];
arr.unshift(0);
arr now has the value [0, 1, 2, 3]
Use splice() method.
First argument: the index at which to begin deleting elements
Second argument (optional): the number of elements to be deleted
Third argument (optional): items to insert starting at that same index, searated by a comma
For instance (example from FreeCodeCamp):
const numbers = [10, 11, 12, 12, 15];
const startIndex = 3;
const amountToDelete = 1;
numbers.splice(startIndex, amountToDelete, 13, 14);
console.log(numbers); // returns [ 10, 11, 12, 13, 14, 15 ]
User spread operator (...). For instance (example from FreeCodeCamp):
let arrayToAppend = ['sage', 'rosemary', 'parsley', 'thyme'];
let mainArray = ['basil', 'cilantro', ...arrayToAppend, 'coriander'];
mainArray now has the value ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']
Use pop() method. For instance:
const arr = [1, 2, 3];
arr.pop();
arr now has the value [1, 2]
Use shift() method. For instance:
const arr = [1, 2, 3];
arr.shift();
arr now has the value [2, 3]
Note: removed data can be returned if assigned to a variable. For instance:
const arr = [1, 2, 3];
let removedNumber = arr.pop();
let one = arr.shift();
removedNumber has the number 3 as a value and one, 1
You can also use splice() method!
First argument: the index at which to begin deleting elements
Second argument (optional): the number of elements to be deleted
For instance (from FreeCodeCamp):
let array = ['today', 'was', 'not', 'so', 'great'];
array.splice(2, 2); // array now has the value ['today', 'was', 'great']
Here we remove 2 elements, beginning with the third element (at index 2). array would have the value
Note: removed data can be returned if assigned to a variable.
I realized it was not working like classic variables like these:
let a = "first variable"; // a contains "first variable" string
let b = a; // a and b contain "first variable" string
b = "second variable"; // a contains "first variable" and b "second variable" string
let array1 = [1, 2, 3]; // array1 contains [1, 2, 3] array
let array2 = array1; // array1 and array2 contain [1, 2, 3] array
array2.unshift(0); // array1 and array2 contain [0, 1, 2, 3] array
Here, array2 behaves just like another pointer to the same object. Modifying array2 will modify array1 and inversely. So how can we copy an array? Here are several methods:
For instance:
let originalArray = ['great', 'awesome', 'super', 'wonderful'];
let copiedArray = [...originalArray];
originalArray remains unchanged and copiedArray has the same elements as originalArray.
With slice() method, we can copy or extract a given number of elements to a new array, leaving the original array unchanged. It takes 2 parameters:
First argument: the index at which to begin extraction
Second argument (optional): the index at which to stop extraction (not included)
For instance (example from FreeCodeCamp):
let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3); // todaysWeather is ['snow', 'sleet']
some() methods accepts a callback function which should take an element of the array as the argument. It will will return true
if the callback function returns true
for at least one element in the array.
Again, this article has to do with understanding default display value before attempting changes. An HTML element could be either inline or block.
Most used inline elements: a, img, br, span, input, label
Often used inline elements: button, code, em, strong, textarea, select
Most used block elements:
Change default display
Diplay can be decomposed in two directions: outer (element's participation in flow layout) and inner (sets layout of children).
Use display property and set it to block or inline - this defines outer direction
Depending on your intent, you may also want to consider using flex value to manage the inner direction
Note that you can use precomposed values containing both outer and then inner instructions such as inline-flex or inline-block.
Zoom on inline-block
Using inline-block you can transform a block element into an inline element, with a width set. Just remember that your two elements must be on the same line to fit into a 100% width. Use a wrapper to set space between elements. Full demo can be found on CodePen. In the following example, p elements will be displayed side by side.
CSS
p {
display: inline-block;
width: 50%;
}
HTML
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nec neque ut dolor vehicula malesuada.</p><p>Aliquam ornare, orci elementum gravida congue, augue eros congue diam, nec vehicula velit velit non
orci. In eget sem nunc. Nulla facilisi.</p>