If you redeclare a JavaScript variable, it will not lose its original value.
| var x=5; var x; |
After the execution of the statements above, the variable x will still have the value of 5. The value of x is not reset (or cleared) when you redeclare it.
If you redeclare a JavaScript variable, it will not lose its original value.
| var x=5; var x; |
After the execution of the statements above, the variable x will still have the value of 5. The value of x is not reset (or cleared) when you redeclare it.
If you assign values to variables that have not yet been declared, the variables will automatically be declared.
These statements:
| x=5; carname="Volvo"; |
have the same effect as:
var x=5;Creating variables in JavaScript is most often referred to as "declaring" variables.
You can declare JavaScript variables with the var statement:
| var x; var carname; |
After the declaration shown above, the variables are empty (they have no values yet).
However, you can also assign values to the variables when you declare them:
| var x=5; var carname="Volvo"; |
After the execution of the statements above, the variable x will hold the value 5, and carname will hold the value Volvo.
Note: When you assign a text value to a variable, use quotes around the value.
As with algebra, JavaScript variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carname.
Rules for JavaScript variable names:
Note: Because JavaScript is case-sensitive, variable names are case-sensitive.
Do you remember algebra from school? x=5, y=6, z=x+y
Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above to calculate the value of z to be 11?
These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).
A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser what to do.
This JavaScript statement tells the browser to write "Hello Dolly" to the web page:
| document.write("Hello Dolly"); |
It is normal to add a semicolon at the end of each executable statement. Most people think this is a good programming practice, and most often you will see this in JavaScript examples on the web.
The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end of the line as the end of the statement. Because of this you will often see examples without the semicolon at the end.
Note: Using semicolons makes it possible to write multiple statements on one line.
The example below shows how to use JavaSript to write text on a web page:
<html>
<body>
<script type=" text/javascript">
document.write(" Hello World!");
</script>
</body>
</html>
The example below shows how to add HTML tags to the JavaScript:
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>
JavaScript's official name is ECMAScript.
ECMAScript is developed and maintained by the ECMA organization.
ECMA-262 is the official JavaScript standard.
The language was invented by Brendan Eich at Netscape (with Navigator 2.0), and has appeared in all Netscape and Microsoft browsers since 1996.
The development of ECMA-262 started in 1996, and the first edition of was adopted by the ECMA General Assembly in June 1997.
The standard was approved as an international ISO (ISO/IEC 16262) standard in 1998.
The development of the standard is still in progress.
NO!
Java and JavaScript are two completely different languages in both concept and design!
Java (developed by Sun Microsystems) is a powerful and much more complex programming language - in the same category as C and C++.
JScript is Microsoft's implementation of JavaScript.
A Little History...
In typical Web fashion, the evolutionary process of JavaScript has taken a somewhat lumpy path. When JavaScript started out life at Netscape, it was called LiveScript and its goal was to allow dynamic ("live") changes of Web pages and communication between the Web browser and plug-in applications ... such as Java. Java was hot and marketable, so in late 1995 Netscape got permission from Sun, the creators of Java, to use the word "Java" and Livescript became the *catchier* JavaScript.
But along the way, JavaScript took on a life of its own. It was easier for non-programmers to learn than Java. It could add a lot of interactivity with little overhead. Like HTML code, JavaScript is contained within the HTML text file, so it can be easily added and modified. It became hot itself. And so now there's a move afoot to standardize JavaScript.
More Than You Might Want to Know, But...
Within the Web environment, the most common use of Java is to build applets, small programs that launch automatically from within a Web page. When the Web page loads, these applets are launched and begin to run on your local computer. While the applet runs, your browser sits quietly by.
On the plus side, running a Java applet doesn't consume server time. It makes it possible for small, self-contained programs to be delivered via the Web, but run locally on individual computers. However, downloading Java applets has some people concerned about security; in corporate environments, Information Systems departments sometimes set up procedures to screen out external Java applets and prevent them from being downloaded. These IS managers fear (rightly or wrongly) that the applet could infect the system or be, in some way, a threat to the network.
JavaScript, on the other hand, is attractive to Web developers because it is a scripting language rather than a self-executing program. The code is typically placed directly in the Web page—in fact, it requires a Web browser in order to run and it is designed to be fully-integrated with the browser. Because the JavaScript is physically located within the Web page, firewalls can't screen it out (although individual browsers can be configured to turn off JavaScript).
JavaScript is an object-oriented scripting language that lets you build interactions between page content, the state of the browser, and the actions of the reader.
In other words, JavaScript is a language that lets you make your pages interact with your readers and respond to what they do.
It is a programming language that can make your pages feel more dynamic and give feedback to your user. This is a good argument for learning and incorporating it into your web techniques basket.
It is a programming language that also has a learning curve in order for you to use it well. But don't let this scare you away from it—if you aren't a programmer, you can start out by learning a few basic concepts and then adapt existing JavaScript scripts for your needs.
If you don't want to tackle JavaScript, that's OK too. You can still make attractive pages that your readers will like, using standard HTML. Don't let the "flavor of the month" mentality convince you that only programmers or big budget outfits can have a Web page. But take a look through this section—you might surprise yourself by knowing more about scripting than you think.
Before you continue you should have a basic understanding of the following: