Board Thread:General Discussion/@comment-5590118-20130529212100

Hello and welcome all! Today i'll tell you a little bit about javascript. I'll cover how to make a simple program. Let us get started shall we.

Things needed:


 * A web browser

Okay, the first thing you need for this is a variable. A variable is what we can use to store information in javascript. We can create a variable by doing the following:

var test = 5;

To break this down:

var

this word tells the browser that we are going to store data.

test

this is the name of the variable, this may be named whatever you wish.

=

This tells the browser that variable "test" will store the following information.

5

This is the data that variable "test" will store, this can also be changed to anything you want.



tells the browser that we have finished declaring our variable and it can go on to the next line of code.

okay so now that we have variables let me tell you how to store different types of data in it. say you want to store the sentece: Hello World!

you need to surround the sentence with single quotes like such: 'Hello World!'

the resulting variable would look as follows: var test = 'Hello World!';

But lets mix it up and say you want to store thhe sentence: I didn't do it!

we will run into problems because of the single quote in didn't, so to prevent this we surround the sentence with double quotes as follows: "I didn't do it!"

and the resulting variable declaration would look as follows: var test = "I didn't do it!";

Now that we have variables down lets do soething with one! For this next part we are going to create a popup that asks your name and then creates another popup that says hello.

now to start we will need to use the prompt; trigger. The text inside of the prompt follows the same rules as the variables do.

so lets create a popup that asks for your name and stores it as a variable!

var name = prompt("What's your name?");

this line takes the results from prompt and assigns it's value to variable name.

now let us create a pop up saying hello:

alert("Hello"+name);

now you may be thinkin "hey hold up! whats with the addition signs and only surrounding hello with quotes?". Well closing the quotes around hello tells it to always print hello, and +name means it needs to add in the value from variable name.

the full program is below:

var name = prompt("What's your name?");

alert("Hello"+name);

Now how do we use this code? Well if you are using Google Chrome you need to d the following:

f12 > console > copy+paste the code > hit enter!

Firefox:

ctrl+shift+k > copy+paste the code >hit enter!

Now try tweaking the code to do other things and posting your creations below so that others may learn from it! 