JavaScript has programming capabilities similar to the most programming languages such as variables, data types, constants, programming constructs, user defined functions etc..
Characteristics of JavaScript
1. JavaScript is Case Sensitive: Unlike HTML, JavaScript is case sensitive. You need to take care when you write javaScript statements, create or call variables, objects and functions.
2. JavaScript Statements: JavaScript is a sequence of statements that are to be executed by the browser. It is a command to the browser which tells browser what to needs to do. The following JavaScript statement tell the browser to write "Hello" on the web page:
document.write("Hello");
3. You need to add a semicolon at the end of each executable statement. According to the JavaScript standard semicolon is optional and the browser is supposed to interpret the end of the line as the end of the statement. Using semicolons makes it possible to write multiple statements on one line. Also it is a good programming practice.
4. JavaScript Code: JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is executed by the browser in the sequence they are written. Following code will write a header and a paragraph on the web page:
5. JavaScript Blocks: You can group JavaScript statements in blocks. Such blocks start with a left curly bracket {, and ends with a right curly bracket }. The purpose of a block is to make the sequence of statements execute together. Following example will write a header and paragraphs to a web page:
JavaScript variables
Variables are "containers" for storing information. A variable can have a short name, like x, or a more descriptive name, like carname. Rules for JavaScript variable names:
• Variable names are case sensitive (y and Y are two different variables)
• Variable names must begin with a letter or the underscore character
Note: Because JavaScript is case-sensitive, variable names are case-sensitive.
A variable's value can change during the execution of a script. You can refer to a variable by its name to display or change its value.
Creating Variables in JavaScript
Creating variables in JavaScript is done using var statement [It is most often referred to as declaration of variables].
E.g. Declaration of Variables
var y;
var name;
In the above examples two variables y and name have been declared. Note that these variables are empty as no value has been assigned to them. You can assign values to them as follows:
var y=4;
var name="Kanchi";
In this example y has been assigned value 4 and name has assigned string “Kanchi”. After the execution of the statements above the variable y will hold the value 4, and name will hold the value Kanchi.
Note: While assigning text value to a variable, use quotes around the value.
In a JavaScript you do not need to explicitly declare a variable as this can be done by initializing a variable with some value. However, as a good programming style it is recommended that you declare a variable in a javaScript. Declaration of a variable in javaScript tells javaScript interpreter that the variable exists and can be referenced through out the document. Declaration of variable ensures that the program is well organized and helps to keep track of the scope of the variables in the program.
Assigning Values to Undeclared Variables
In JavaScript you can assign values to the variables that have not yet been declared. In such case variables will automatically be declared. Check out the above example once again. The statements:
y=4;
name="Kanchi";
have the same effect as:
var y=5;
var name="Kanchi";
Re-declaring Variables in JavaScript
In case you re-declare a variable in JavaScript you will not lose its original value.
E.g. Redeclaring Variables
var y=4;
var y;
In the above example y has been re-declared however it will have the value of 5. The value of y is not reset (or cleared) due to re-declaration
Comments
Vee Eee Technologies| Vee Eee Technologies|