Skip to content

Instantly share code, notes, and snippets.

@0ex-d
Last active December 27, 2021 11:20
Show Gist options
  • Save 0ex-d/6fd2e630913023a36fd586c832cb297d to your computer and use it in GitHub Desktop.
Save 0ex-d/6fd2e630913023a36fd586c832cb297d to your computer and use it in GitHub Desktop.
Simple snippets for beginners in Javascript
// the below are snippets in JavaScript
// they are independent and have been broken
// into smaller functions
// it is likely you'll use them in your code
//
// Remember: no matter how small or big a project maybe
// it still comprises of one or more pieces coming together
// it is just how you use it (LOGIC) to solve a problem.
//
// Arrays & Objects powers the world
// the search results on Google, the list posts on instagram are all Arrays
// no magic about all these data.
function declare_const_let() {
const a = 1;
let b = 'Seun';
}
function output_print_statement() {
console.log('Can you see me?');
}
function output_print_statement_with_special_string() {
// notice how we use the ` sign rather than ''
// this means special string formatting
const myName = 'Seun';
// the ${} is used for formatting the variable
// to be passed between the `
console.log(`Can you see me ${myName}?`);
// another example:
const friends = ['Seyi', 'Joy'];
console.log(`I have ${friends.length} friends`);
}
// ARRAY:
function declare_an_empty_array() {
const a = [];
}
function declare_an_non_empty_array() {
const a = ['NG', 'UK', 'US'];
}
function get_the_length_of_array() {
const a = ['NG', 'UK', 'US'];
return a.length;
}
function return_elements_in_array() {
const a = [];
a.forEach(function (item) {
// loop through the array
console.log(item); // every loop should output an item
});
}
function add_items_to_array() {
const a = [];
a.push('James'); // single add
a.push('Emmy', 'Bola', 'Kola'); // add multiple at once
}
function add_items_to_array_using_loop() {
const a = [];
a.forEach(function (item) {
// loop through the array
a.push(item); // every loop should 'push'/add an item to it
});
}
// RELATIVE LOGIC
// boolean
console.log(true === true);
console.log(true === false);
console.log(false === false);
// others
console.log(1 > 2);
console.log(1 > 2);
console.log('Dolapo' === 'Dolapo');
// CONDITIONALS
function is_the_light_on() {
let status_of_light = true;
if (status_of_light) {
console.log('The light is on');
} else {
console.log('The light is off');
}
}
// can be re-written as
function is_the_light_on_2() {
let status_of_light = true;
if (!status_of_light) {
console.log('The light is off');
} else {
console.log('The light is on');
}
}
function is_x_older_than_y(x, y) {
// now you can see how we use comparison logic here
// remember: small pieces form bigger solutions
if (x > y) {
console.log('x is older sibling');
} else if (y > x) {
console.log('y is older sibling');
}
}
// you can also use if/else
function is_x_older_than_y(x, y) {
if (x > y) {
console.log('x is older sibling');
} else {
console.log('y is older sibling');
}
}
// FUNCTIONS
function accountBalance() {
// parameters here
// function body
// this is where logic stays
// you can out put directly using console.log()
// OR you can just use 'return' statement
}
function accountBalanceA() {
return 100;
}
// don't forget to initiate your function
accountBalance();
// sometime you want to print the value
console.log(accountBalance());
// OR you may want to use it somewhere else
const withdrawalAmount = 100;
function withdrawalMoney(amount) {
return accountBalanceA() - amount;
}
withdrawalMoney(withdrawalAmount);
// Bonus:
// Functions can be rewritten using arrow functions
// they look like this '() =>'
//
// i.e
//
// function accountBalance() {
// return 100;
// }
//
// BECOMES
//
// const accountBalance = () => {
// return 100;
// }
//
// AND
//
// function withdrawalMoney(amount){}
//
// BECOMES
//
// const withdrawalMoney = (amount) => {}
//
// Don't worry, you don't have to bother too much
// as you practice you'll get used to it :)
//
// just putting it here
// in case you come across it in other people's project ;)
// examples where arrow functions are also used
a.forEach((item) => {});
a.map((item) => {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment