Skip to content

Instantly share code, notes, and snippets.

View suhailgupta03's full-sized avatar
:octocat:

Suhail Gupta suhailgupta03

:octocat:
View GitHub Profile
function greet(message, username) {
// undefined are FALSY values in JS
// !undefined means !false -> true
if(!username) { // if username is not yet defined
username = "user"; // we are assinging it some value
}
var greetMessage = `Hello, ${username}! ${message}`;
console.log(greetMessage);
}
greet("Good evening");
var totalSum = 0; // this is variable with global scope
function sumTwoNumbers(x, y) {
if(!y) { // this checks if y is undefined
// if y is undefined, it will not
// enter if block
totalSum = totalSum + x;
}else {
totalSum = x + y;
// here total sum becomes 300
var x = undefined;
console.log(x); // undefined
console.log(!x); // undefined in JS is a FALSY value
// !false means true
if(!x) { // since this is true, it will execute if block
console.log("inside if");
}else {
console.log("inside else");
}
function sum(x, y) {
var z = x + y;
return z; // return back the value of
// z
}
var zz = sum(5 , 6);
// sum(5 , 6) evaluates to 11
// sum(10, 20) evaluates to 30
console.log(zz);
var x = 7;
console.log(x); // 7
console.log(y); // undefined
var y; // Because of hoisting, the variable y
// will be PULLED upwards just before the program
// starts its execution
sum(); // sum func will be pulled up b.cz of hoisting
mult(); // mult func will be pulled up b.cz of hoisting
div(); // the division function will be pulled up because of
// hoisting
function sum() {
console.log("sum");
}
var zz = function(x, y) {
return x + y;
} // Arjun created this general definition (anonymous) for other
// developers
var addTwoNumbers = zz; // Aditi says, that I will start
// using zz to add two numbers, but before I will give it
// a proper name
var result = addTwoNumbers(1, 2);
console.log(result);
var xx = () => {
return "hey there!!"
}
var f = xx();
console.log(f);
function multiply(a, b) {
return a * b;
}
function square(n) {
return multiply(n, n);
}
function printSquare(n) {
let result = square(n);
function greet() {
console.log("Good evening"); // print the message
greet(); // call greet function
}
greet();