Monday, February 1, 2010

JS: Variables and Operators

Variables

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:
  • Variable names are case sensitive(y ans Y are two different variables)
  • Variable names must begin with a letter or the underscore character.


Declaring(creating) JavaScript Varibales

Creating variables in JavaScript is most often referred to as "declaring" varibles. 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). Values can be assigned to variables at the time of declaration or even at a later point of time. If undeclared variables are assigned with some values, then the variables will automatically be declared.

Redeclaring JavaScript Variables

If by chance some already used variable names are redeclared, then it will not lose its original value.

Note: JavaScript allows you to perform arithmetic operations with JavaScript variables.

JavaScript Operators


JavaScript supports regular Arithmetic and Assignment operators that we see in Java. Below, we have a set of operators given in a table:

Arithmetic Operators

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Divsion
%Modulus
++Increment
--Decrement


The + Operator Used on Strings


The + operator when used with two strings performs concatenation. When a + operator is used between a string and number, the result will be a string again.

Comparison Operators

All the comparison operators used in java are applied here, with the addition of one new operator:
===   -   is exactly equal to(both 'value' and 'type')
Example:
x===5 is true
x==="5" is false
Comparison operators are commonly used with conditional statements.

Other Operators

The remaining operators include the logical and conditional operators. &&, ||, ! are the three logical operators.


link

No comments:

Post a Comment