Variables
- The keyword var is used to define a variable.
- Variable names can contain numbers, the dollar symbol ($) and underscores (_)
- Variable names can begin with $ or _ but not numbers
- In JavaScript strings can be enclosed in single quotes or double quotes. I guess you could use single quotes quotes whenever you need double quotes inside a string.
Examples
A String - var question1 = "What is Kenny's address?"
An Integer - var int = 728
A Float - var $pr = 7.28
Concatenation
Basically this is combining strings with a plus symbol.
Examples
var firstName = "Victor";
var lastName = "Wooding";
var fullName = firstName + lastName;
Updating a Variable
This is a very important method that you will use frequently in JavaScript
Example
var message = "John Brown wears a bowtie.";
message += " He also likes to wear suspenders."
message += " John Brown is a good man.";
alert(message);
What will the alert message print?
Getting User Input
It is possible to get the user to enter information using the prompt() function.
You could use it to ask a question and store the response in a variable
Example
var feeling = prompt('Use one word to describe how you feel.');
alert("Victor feels " + feeling + " today!");
What do the above lines of code print?
The prompt method always requires a string.
Writing to the Webpage
It is possible to write to the webpage using the document.write() function.
Example
Continuing from above you could write.
var feeling = prompt('Use one word to describe how you feel.');
document.write("Victor feels " + feeling + " today!");
you could adjust the size of the text by enclosing it in a header. Pay close attention to the syntax.
document.write("<h3>Victor feels " + feeling + " today!</h3>");
No comments:
Post a Comment