Sie sind auf Seite 1von 5

Java Script Basics -

Alert = Alert is a command that's built into the browser. It opens a dialogue box and
displays a message. It is a JavaScript statement.

Example: alert(Hello Word!);

A statement is like a sentence. And just as sentences end in a period, JavaScript


statements end in a semicolon. You write programs by typing multiple statements. Just
like you write a paragraph, by writing multiple sentences.

document.write = The word document represents the current web page, and write is a
command that writes a message to the page.

Example: document.write(Hello World!);

a program is a set of statements that run one after the other. one statement needs to
be complete before the next statement runs. So the document.write command which
prints to the page can't do it's thing until the first alert command, which includes the
user closing that dialogue box, finishes.

Browsers perform lots of different functions. They read and display content using HTML. They
style that HTML following CSS rules. And they add interactivity to a page by follwing the
instructions in a JavaScript program.
Ever browser has something called a JavaScript interpreter built into it. This is the part of the
browser that reads and understands, and runs the instructions in a JavaScript program.
When a browser encounter JavaScript programming, the JavaScript interpreter looks at each
statement in the program and does what the statement says to do. That is, your program runs as
the JavaScript interpreter reads it..
When a browser reads and acts on a JavaScript program, that is called executing the program.
It is common to put all your JavaScript code in a .js files separate from the HTML files and then
connect them through and HTML scipt tag.

You link the .js and html in the following manner -

Link a JavaScript file to a web page using the <script> tag and
the src property:

<scriptsrc="scripts.js"></script>

Insert JavaScript directly into a web page inside <script> tags:

<script>
alert(Hellothere.);
</script>
Never link to a file and include JavaScript inside the same <script> tag.

Don't do this:

<scriptsrc="file.js">
alert("Hellothere.");
</script>

You can place the script tags almost anywhere in a Web page, but most typically, you'll find
script tags placed in either the head of the page, usally just before the closing head, or within the
body of the page just before the closing body tag.

One advantage of placing you script near the bottom of the page is that it lets the browser load
and display any HTML before running the JavaScript.

You can view the JavaScript Console in Chrome by pressing ctrl+shift+j. This can be used to
find syntax errors on a line to line basis.

You can also print out a console log using console.log(****); in your .js file.

Variables -

A variable is a way of storing and keeping track of information in a program.

In order for our program to follow all a variable, we need a way to identify each
variable in a program. That's why each variable has its own name, like score, which
identifies that one variable..

The JavaScript word for creating a variable is VAR.

We follow that with a space and a name for that variable. When you create a variable,
you can leave it empty. In that case, add a semicolon to end the statement. Or you can
create the variable and put something into it in a single statement. Using the equal
sign, you can insert a value into a variable, var score = 0. The equal sign tells the
JavaScript interpreter to put whatever's on the right side into the name on the left side.

Example: var message = This is a pancake!

When we place something in a variable, we call that assigning a value to the variable.

We use quotes to indicate the actual letters, numbers and

A series of characters inside of quote marks are called a string.

To get at the contents of the variable, we just type its name. For example, we can re,
replace this Welcome to Treehouse inside the alert command with the variable named
message. Quotes are not needed when using your variable with alert(*);

When you use the variable later in your program or change the variable's contents, you
don't need to use the var keyword again. The equal sign assigns a new string to the
variable and with this you can change the variables contents later in the program.

Variable names can not include a JavaScript keyword or begin with a number. Names
may also only contain letters, number, dollar sign $, or an underscore.

Many programmers will create variable names but using underscores such as
price_per_unit or with camel casing such as pricePerUnit.

So variables are like boxes that you can store information in. But what exactly are we
storing? We call the information we put in a variable a value, and values come in many
different types.

Variables have two common value types - numbers and strings.

Numbers are used for making calculations, adding, subtracting, computing total costs,
keeping track of a game score and so on.

Strings are used for words, sentences, and other text. A string is just a series of letters,
numbers and other characters inside of quote marks. The quote marks tell the
JavaScript interpreter that it should treat the contents inside as just a set of characters
without any special meaning to the browser.

Each time you wanna add a message to a web page, pop up an alert or collect
information from a web form you'll be dealing with strings.

JavaScript lets you use either double or single quotes to create a string. If you begin a
string with a single quote you need to end the string with a single quote. Likewise if
you begin a string with a double quote you need to end it with a double quote.

Things get a little tricky when you want to put a quote mark inside a string. If you
wanted to put she's a great person in a string you need to look out for the single quote
mark in she's. The single quote starts the string. But the next single quote in she's
ends the string, at least according to the browser's JavaScript interpreter. So the rest of
the characters are outside the string and cause an error. A simple solution is to use
double quotes to create a string that has one or more single quotes in it. Because the
first quote mark is a double quote, the JavaScript interpreter won't end the string until
it finds the next double quote mark. Likewise, you can use single quotes to create a
string whenever you need double quotes inside the string.

There's another way to put a quote into a string. You can use what's called an escape
character. If you put a backslash before the quote mark, the JavaScript interpreter
simply treats the quote mark like any other character with no special power. It's
literally just a quote mark character at that point. You can use the backslash before
either single or double quote marks.

The prompt command lets you ask a question and get a response.
Example: prompt(What is your name?);

The prompt command lets the browser capture the user's response and gives it back to the
program. In programming speak, we say that this command returns a value. We can
store a returned value in a variable. In other words, we can capture user input to use in
our program like this.

Here is the process - First, var visitorName creates a new empty box. A new variable
named, visitor name. Second, the prompt opens a dialogue box that the visitor fills out.
Third, the equal sign puts the return value, whatever was typed by the visitor, into the
new variable. And fourth, the alert command looks inside that variable and displays its
value in a pop-up alert box.

Adding strings and variables -

Concatenation - The process of combining strings is called "concatenation". In


JavaScript, you combine strings with a + operator like this:

'one string ' + ' another string'

If you have a variable that contains a string you can combine it with a literal string
value to come up
with a new string:

var name = 'Dave';


var message = 'Hello ' + name;

Now that we have a method for collecting information from a visitor to our site, let's
see how to use that input to create different messages. We'll do this by collecting input
from a browser prompt, adding that to other words, and printing the final message to
the Web page.
When we combine strings together, we literally just place them next to each other. If
we need a space, we have to add it.
Combining strings together is called concatenation. By adding strings together, we end
up with a bigger string. When a variable contains a string, adding that variable to
another string is the same as combining two strings. In other words, the variable is
really a placeholder for the string inside it. You can add any number of strings together.
In programming, you sometimes want to create a really long string, maybe an entire
paragraph of text that includes string values from a bunch of different variables. You
can use the plus operator over and over, but sometimes that can be hard to read. To
make your code easier to read, you can create a longer string using a series of smaller
statements.

Example: message = message + ' Hello there';

This might look a bit strange. The variable message appears twice in the statement. So
how does this work? Well, remember when putting a value into a variable, the stuff on
the right goes into the variable on the left. In this case, the stuff on the right is the
current contents of the variable message added to the string, We are so glad that you
came by to visit. These two strings are combined and then the result is placed back
into the variable message. In other words, we first access the value inside the variable,
then update the value of that same variable.
Updating variables is common, so common there is a built in short hand method for
adding to the contents of a variable.

Example: var visitor = prompt('What is your name?');


var message = 'Hello ' + visitor + '. Welcome to
Treehouse.';
message += 'We are so glad that you came by to visit, ';
message += visitor;
message += '. Please come again.';
document.write(message);

Plus equals is the same as equals message plus. Plus equals means take the contents
of the variable and add to it whatever appears on the right of the equal sign.

Das könnte Ihnen auch gefallen