Adventures in JavaScript
Wednesday, February 9, 2011
Rules for Naming:
-An identifier must be at least one character in length
-The first character must be a letter, underscore ( _ ), or a dollar sign ($).
-Each character after the first can be a letter, underscore, dollar sign or a number.
-Spaces and special characters other than a _ and $ are not allowed in any part of an identifier.
*Variable names often use CamelCase
A good way to name variables with multiple words is with lowerCamelCase.
-The first character must be a letter, underscore ( _ ), or a dollar sign ($).
-Each character after the first can be a letter, underscore, dollar sign or a number.
-Spaces and special characters other than a _ and $ are not allowed in any part of an identifier.
*Variable names often use CamelCase
A good way to name variables with multiple words is with lowerCamelCase.
Constants
To store a piece of data that can never change, you need a constant.
Constants are created just like initialized variables, but you use the const keyword instead of var.
And the "initial" value becomes the permanent value.
Example:
const + constant name + = + constant value + ;
The syntax is the same but constants are usually named in all capital letters to make them STANDOUT in your code.
These are handy for storing information that you might directly code into a script, like a sales tax rate. Instead of using a number, your code is easier to understand if you use a constant with a descriptive name like TAXRATE.
And if you need to make changes to the value, you can do it in one place...where the constant is defined instead of locating it and having to change it throughout the document.
Constants are created just like initialized variables, but you use the const keyword instead of var.
And the "initial" value becomes the permanent value.
Example:
const + constant name + = + constant value + ;
The syntax is the same but constants are usually named in all capital letters to make them STANDOUT in your code.
These are handy for storing information that you might directly code into a script, like a sales tax rate. Instead of using a number, your code is easier to understand if you use a constant with a descriptive name like TAXRATE.
And if you need to make changes to the value, you can do it in one place...where the constant is defined instead of locating it and having to change it throughout the document.
Data Types
JavaScript uses three basic data types.
1. text
2. number
3. boolean
Text
Text is usually words or sentences, but it doesn't have to be.
Also known as strings, JavaScript text always appears within quotes(" ") or apostrophes(' ').
Number
Numbers are used to store numeric data like the weights and quantities of things. JavaScript numbers can be either integer/whole numbers or decimals.
Boolean
Boolean data is always in one of two possible states--true or false.
You can use this to represent anything that has two possible settings.
1. text
2. number
3. boolean
Text
Text is usually words or sentences, but it doesn't have to be.
Also known as strings, JavaScript text always appears within quotes(" ") or apostrophes(' ').
Number
Numbers are used to store numeric data like the weights and quantities of things. JavaScript numbers can be either integer/whole numbers or decimals.
Boolean
Boolean data is always in one of two possible states--true or false.
You can use this to represent anything that has two possible settings.
Variables
A variable is a storage locaion in memory with a unique name, like a label on a box that is used to store things.
You create a variable using a js keyword var, and the name of the new variable.
Example:
var + variable name + ;
The var keyword indicates that you're creating a new variable.
The variable name can be just about anything as long as its unique within your script.
The semicolon ends the line of javascript code.
A newly created variable has reserved storage space set aside and it is ready to store data
The key to accessing and manipulating its data is in its name.
That is why its important for the name of every variable to be unique and meaningful!!!
Initialize a variable with "="
Example:
var + variable name + = + initial value + ;
The equals sign connects the variable name to its initial value
The initial value is stored in the variable
var population=300;
This line of script also assigns the data type of the variable as a number because it was given a numeric value, 300. If the variable changes to some other type, then the type of the variable changes to reflect the new data. Most of the time js handles this automatically.
You create a variable using a js keyword var, and the name of the new variable.
Example:
var + variable name + ;
The var keyword indicates that you're creating a new variable.
The variable name can be just about anything as long as its unique within your script.
The semicolon ends the line of javascript code.
A newly created variable has reserved storage space set aside and it is ready to store data
The key to accessing and manipulating its data is in its name.
That is why its important for the name of every variable to be unique and meaningful!!!
Initialize a variable with "="
Example:
var + variable name + = + initial value + ;
The equals sign connects the variable name to its initial value
The initial value is stored in the variable
var population=300;
This line of script also assigns the data type of the variable as a number because it was given a numeric value, 300. If the variable changes to some other type, then the type of the variable changes to reflect the new data. Most of the time js handles this automatically.
Constants and Variables: Overview
Storing data in JavaScript isn't just about type, it's also about purpose. What do you want to do with the data? Or more specifically, will the data change throughout the course of your script? The answers determine whether you code your data type in JavaScript as a variable or a constant.
A variable changes throughout the course of a script, while a constant never changes its value.
A variable changes throughout the course of a script, while a constant never changes its value.
Posting Code
Remember this site if you ever want to post code....and you actually want to see the code you post.
http://www.simplebits.com/cgi-bin/simplecode.pl?mode=process
http://www.simplebits.com/cgi-bin/simplecode.pl?mode=process
Subscribe to:
Posts (Atom)