Types
Javascript has nine types. They are:
1. Null –’ is a keyword in JavaScript that signifies ‘no value’ or nonexistence of any value.
2. Undefined – undefined. What you get if you ask an object for something it doesn’t have.
3. Strings – e.g. ’Abc’, "Abc" (single vs. double quotation marks makes no difference).
4. Numbers – JavaScript has only one type of number. Numbers can be
written with or without decimals.
written with or without decimals.
5. Booleans – represents one of two values: true or false. ex:Boolean(10 > 9)
// returns true
// returns true
(10 > 9) // also returns true
10 > 9 // also returns true
6. Arrays – JavaScript arrays are used to store multiple values in a
single variable e.g. var arr = [1, 2, 3, 4];
single variable e.g. var arr = [1, 2, 3, 4];
7. Objects – Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to a variable named car:
This code assigns many values (Fiat, 500, white) to a variable named car:
var car = {type:"Fiat", model:"500", color:"white"};
8. Regular expressions – Regular expressions are patterns used to match character
combinations in strings.
combinations in strings.
Ex: var regex = /ca[kf]e/;
var str = "He was eating cake in the cafe.";
if(regex.test(str)) {
document.write("Match found!");
} else {
document.write("Match not found.");
}
9. Functions – function is defined with the function keyword, followed by a name,
followed by parentheses ().
followed by parentheses ().
e.g. var x = myFunction(4, 3);
function myFunction(a, b) {
return a * b;
}
null
The value null is actually almost never produced by Javascript. The only case you’re likely to run across null is if you assign it somewhere (most of the time you’ll get undefined instead – one notable exception is document.getElementById, which returns null if it can’t find an element). Making sparing use of undefined and instead using null can make bugs much easier to track down.
Case Sensitivity
JavaScript is a case-sensitive language. This means that the language keywords,
variables, function names, and any other identifiers must always be typed with a
consistent capitalization of letters
variables, function names, and any other identifiers must always be typed with a
consistent capitalization of letters
Variables
Variables can be thought of as named containers. You can place data into these containers
and then refer to the data simply by naming the container. Before you use a variable in
a JavaScript program, you must declare it. Variables are declared with the var keyword.
and then refer to the data simply by naming the container. Before you use a variable in
a JavaScript program, you must declare it. Variables are declared with the var keyword.
Global Variables: A global variable has global scope which means it can be defined
anywhere in your JavaScript code. Local Variables: A local variable will be visible
only within a function where it is defined. Function parameters are always
local to that function.
anywhere in your JavaScript code. Local Variables: A local variable will be visible
only within a function where it is defined. Function parameters are always
local to that function.
Reserved Words
A list of all the reserved words in JavaScript are given in the following table.
They cannot be used as JavaScript variables, functions, methods, loop labels, or
any object names
OPERATORS
- JavaScript supports the following types of operators.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
Arithmetic Operators
Assume variable A holds 10 and variable B holds 20, then:
+ (Addition) Adds two operands
Ex: A + B will give 30
- (Subtraction) Subtracts the second operand from the first Ex: A - B will give -10
* (Multiplication) Multiply both operands
Ex: A * B will give 200
/ (Division) Divide the numerator by the denominator
Ex: B / A will give 2
% (Modulus) Outputs the remainder of an integer division
Ex: B % A will give 0
++ (Increment) Increases an integer value by one
Ex: A++ will give 11
-- (Decrement) Decreases an integer value by one Ex: A-- will give 9
/ (Division) Divide the numerator by the denominator
Ex: B / A will give 2
% (Modulus) Outputs the remainder of an integer division
Ex: B % A will give 0
++ (Increment) Increases an integer value by one
Ex: A++ will give 11
-- (Decrement) Decreases an integer value by one Ex: A-- will give 9
Comparison Operators
Assume variable A holds 10 and variable B holds 20, then:
== (Equal) Checks if the value of two operands are equal or not, if yes,
then the condition becomes true.
then the condition becomes true.
Ex: (A == B) is not true.
!= (Not Equal) Checks if the value of two operands are equal or not,
if the values are not equal, then the condition becomes true.
if the values are not equal, then the condition becomes true.
Ex: (A != B) is true.
> (Greater than) Checks if the value of the left operand is greater than
the value of the right operand, if yes, then the condition becomes true.
the value of the right operand, if yes, then the condition becomes true.
Ex: (A > B) is not true.
< (Less than) Checks if the value of the left operand is less than the
value of the right operand, if yes, then the condition becomes true.
value of the right operand, if yes, then the condition becomes true.
Ex: (A < B) is true.
>= (Greater than or Equal to) Checks if the value of the left operand
is greater than or equal to the value of the right operand, if yes,
then the condition becomes true.
is greater than or equal to the value of the right operand, if yes,
then the condition becomes true.
Ex: (A >= B) is not true.
<= (Less than or Equal to) Checks if the value of the left operand is
less than or equal to the value of the right operand, if yes, then the condition
becomes true.
less than or equal to the value of the right operand, if yes, then the condition
becomes true.
Ex: (A <= B) is true.
Logical Operators
&& (Logical AND)
If both the operands are non-zero, then the condition becomes true.
Ex: (A && B) is true.
|| (Logical OR) If any of the two operands are non-zero, then the condition becomes true.
Ex: (A || B) is true.
! (Logical NOT) Reverses the logical state of its operand. If a condition is true,
then the Logical NOT operator will make it false.
then the Logical NOT operator will make it false.
Ex: ! (A && B) is false.
Conditional Operator (? :)
The conditional operator first evaluates an expression for a true or false value and then
executes one of the two given statements depending upon the result of the evaluation
executes one of the two given statements depending upon the result of the evaluation
? : (Conditional ) If Condition is true? Then value X : Otherwise value Y
if-else
JavaScript supports the following forms of if..else statement −
- if statement
- if...else statement
- if...else if... statement.
if statement The if statement is the fundamental control statement that allows
JavaScript to make decisions and execute statements conditionally.
JavaScript to make decisions and execute statements conditionally.
var age = 20;
if( age > 18 ){
document.write("<b>Qualifies for driving</b>");
}
if..else statement: The 'if...else' statement is the next form of control statement that allows JavaScript to execute statements in a more controlled way
var age = 15;
if( age > 18 ){
document.write("<b>Qualifies for driving</b>");
}
else{
document.write("<b>Does not qualify for driving</b>");
}
if...else if... statement
The if...else if... statement is an advanced form of if…else that allows JavaScript
The if...else if... statement is an advanced form of if…else that allows JavaScript
to make a correct decision out of several conditions.
var book = "maths";
if( book == "history" ){
document.write("<b>History Book</b>");
}
else if( book == "maths" ){
document.write("<b>Maths Book</b>");
}
else if( book == "economics" ){
document.write("<b>Economics Book</b>");
}
else{
document.write("<b>Unknown Book</b>");
}
No comments:
Post a Comment