You’re closer than you think to a career in tech. Our grads have launched rewarding new careers — you can too.View Our Jobs Report
My First Original Function
Define a variable using var variable_name =
A function takes parameters that you can use inside that function
All variable assignments or methods end with a ;
1 Minute Assignment:
Write an original function using the basic pattern above.
// is used to make a comment
is used to begin a multi-line comment
*/ is used to end a multi-line comment
Variables do not begin with a number or hyphen
Functions can only be called when defined as a variable
Define a function as a variable using var or function
For functions within a function, the nested functions should use the parameters provided in the outer functions and not introduce new parameters.
1 Minute Assignment:
Write a function inside a function like above and successfully call it.
Use return to perform math
Wrap a command in the log(); method to print the return value in the console
There can be no return values after errors.
Above, the return values are bar and undefined. The command
If you’re not using a dynamic editor, use
Functions create a scope wherein variables defined within are not acknowledged wihtout. This is why
log( typeof foo)returns undefined, because foo is undefined outside of myFunction. It’s worth noting that the same function without ‘return’ will return undefined because it is outside scope.
Because of variable scope, you can have different variables that share the same name, but exist in different scopes.
Therefore it’s best to name variables uniquely and descriptively to avoid confusion between scopes.
Generally, use var to assign all variables because otherwise, the variable you define will default to a global scope.
Don’t do the above because if you set the variable
a in other places in your code, there will be conflict.
To solidfy your understanding, try the following exercise:
Try the following methods in your browser’s console, as they will prove very useful. Good luck!
Blog Post Author
Since we opened our doors in 2012, thousands of students have joined Flatiron School to launch new careers in tech.
Find the perfect course for you across our in-person and online programs designed to power your career change.
Connect with students and staff at meetups, lectures, and demos – on campus and online.
Have a question about our programs? Our admissions team is here to help.