S 21
S 21
");
//Function declarations
//(function statement) defines a function with the specified parameters.
/*
Syntax:
function functionName() {
code block (statement)
}
*/
// function keyword - used to defined a javascript functions
// functionName - the function name. Functions are named to be able to use
later in the code.
// function block ({}) - the statements which comprise the body of the
function. This is where the code to be executed.
function printName(){
console.log("My name is Janie");
};
printName();
//Declared functions are not executed immediately. They are "saved for
later use", and will be executed later, when they are invoked (called upon).
function declaredFunction() {
console.log("Hello World from declaredFunction()");
};
declaredFunction();
//Function Expression
//A function can also be stored in a variable. This is called a function
expression.
variableFunction();
/*
Scope is the accessibility (visibility) of variables.
console.log(globalVar);
// console.log(localVar); - error (Uncaught ReferenceError: localVar is not
defined)
function showNames(){
console.log(functionVar);
console.log(functionConst);
console.log(functionLet);
showNames();
// console.log(functionVar);
// console.log(functionConst);
// console.log(functionLet);
//Nested Functions
function myNewFunction(){
let name = "Owen";
function nestedFunction(){
let nestedName = "Moon";
console.log(name);
}
// console.log(nestedName);
nestedFunction();
}
myNewFunction();
// nestedFunction(); - error (index.js:126 Uncaught ReferenceError:
nestedFunction is not defined)
function myNewFunction2(){
myNewFunction();
myNewFunction2();
//alert() allows us to show a small window at the top of our browser page to
show information to our users. As opposed to a console.log() which only shows the
message on the console. It allows us to show a short dialog or instruction to our
user. The page will wait until the user dismisses the dialog.
function showSampleAlert(){
alert("Hello User!");
};
// showSampleAlert();
console.log("I will only log in the console when the alert is dismissed");
/*
Notes on using alert()
* Show only an alert() for short dialogs/messages
* Do not overuse alert() because the program has to wait for it
to be dismissed
*/
/*
Syntax:
prompt("<dialogInString>");
*/
function returnFunc(){
return "I am a return statement";
}
function returnName(){
};
function getCourses(){
let courses = ["Science 101", "Math 101", "English 101"];
return courses;
}
console.log(getCourses());
//Avoid generic names to avoid confusion within your code. Use definitive names
function get(){
let name = "Noir"
return name
}
console.log(get());
https://gitlab.com/tuitt/students/batch305/resources/sessions