Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iscott/28beb8c883a12b13064651939d7f393d to your computer and use it in GitHub Desktop.
Save iscott/28beb8c883a12b13064651939d7f393d to your computer and use it in GitHub Desktop.
Javascript functions and conditionals using (){}, and semicolons cheatsheet

Javascript functions and conditionals using (){}, and semicolons


Curly braces {} almost always indicate a block of code. { means START } means END

Keywords like


all expect (), then curly braces. So always write them like this first:

if (){}
while (){}
for (){}
function (){}

then go back and fill in the rest.

Curly braces will usually be put on separate lines. That's ok. Javascript ignores line breaks and spaces so you can use them or not.


Anything in parenthesis () are the parameters/arguments accepted for functions or key information the needed for an if, for, while to run the block of code {} that follows. for example:

if (number==2){
  console.log("The number is equal to 2");


Semicolons indicate the end of a line/statement. Since javascript ignores line breaks and spaces, this is a great way of letting javascript know where the line actually ends.

So you could do this:

var name =

and javascript would know the var line ends after "Ira".

If there is a } (which means end) we don't use a semicolon because the } already indicates that is the end.

lines like:

var name = "Ira";

we would put a semicolon (optional in JS, but not optional in other languages so worth understanding)


A function is just a block of code that you give a name so you can re-run it whenever you want without typing in all the lines of code again. It's like a GOTO in line-numbered languages like basic.

For example if we wanted to make a function that popped up an alert box saying "your password is incorrect", then another one saying "please try again". We would write:

function incorrectPassword(){
  alert("your password is incorrect");
  alert("please try again");

Notice this is just:

function incorrectPassword(){}

with code and line breaks inside the curly braces.

semi colons are optional, so I'm going to stop using them from this point on.

This won't run any of those alerts. It only saves them in memory as a function called incorrectPassword. To run it we need to put this in our code:


Notice in this case we put the parenthesis () after it. That tells javascript to run (aka invoke) it. If we don't put the parenthesis, our function acts just like a variable and dumps out the code for the function as text instead of actually running the code.


If we wanted our function to run differently or dynamically based on extra information we give it, we can use parameters like this:

When we define our function:

function sayHello(name){
  alert("Hello " + name)

parameters act like temporary variables inside the code block {}.

So if we invoke the function and pass in the name "Ira" like this:


It will pop up an alert that says "Hello Ira"

Practice making a function called myName that alerts you with your first name. Run your function.

More practice: Make a function called myNamePlusMore that alerts your first name, then your last name, then says "Welcome!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment