Let’s Code: Variables

Let's Code Header

Hello, and welcome to Let’s Code, where I will be showing you the wonders of coding. Everything online, from websites to games, and even this blog is made up of code. There are many different types of coding languages, just like there are many different kinds of human languages. For this blog, we are going to be focusing on JavaScript, HTML, and CSS. Linked at the bottom is also a free website where you can experiment with the coding you learn here and possible create your own masterpiece. Without further ado, let’s code!

To start off, let’s take a look at JavaScript, HTML, and CSS. In simple terms, JavaScript is the skeleton for our code, HTML is the visual element, and CSS is for style. That being said, JavaScript (the skeleton) is the best place to start.

In JavaScript, the most essential components of the code are variables. Variables are pieces of data that store information. For example, we can have a variable named “myName”, where we store the piece of information: “Stanlee.” When coding, this would be written as:

var myName = “Stanlee”;

In this example, var is understood as declaring a new variable, myName is what we will call that variable, = is telling the computer to store something, “Stanlee” is what the variable will store, and ;  is like a period.

In general, there are three types of values a variable can be assigned to store.

  1. String:

A String is essentially text. When you are declaring a string, you have to put the desired text between quotations.

Example:

var string = “This is a String.”;

  1. Number:

A Number is a numerical value. So, 8, 9, 25, 2354, or 4785 is considered a Number. No quotations are needed.

Example:

var number = 32547;

  1. Boolean:

A Boolean is just a fancy term for true or false. They are often used to give a program parameters that allows it to respond to input from a user. A variable being assigned a boolean would look like:

var boolean = true;

So, with this, you now know how to create a new variable and what different kinds of values you can have that variable store. But, what good is this knowledge if you can’t see if it displayed? No good at all. The simplest way to have the computer display your variable in a pop-up box would be to type:

var show = alert(variable name);

This tells the computer to create a variable named ‘show’ that alerts the user to what is in the parentheses.

Example:

var example = “Hello World!”;

var showExample = alert(example);

Note: You can actually write a string, number, or boolean in the parentheses instead of a variable name, and the pop-up box will display that.

Example:

var display = alert(“I can write anything here!”);

To review, we first learned how to create variables, assign one of three values to them, and one way to display them. Next week, we’ll learn about another essential component of JavaScript, if/else statements. For now, visit jsfiddle to try out some of the things you’ve learned. They’re a great site.

Challenge of the week: If you created the variable,

var challenge = “This is a challenge!”;

how would you assign it a new value such as a number or another string. Leave your answers in the comments below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s