MODULE-1
MODULE-1
1) STATEMENTS:
• A script is a series of instructions that a computer can follow one-by-one.
• Each individual instruction or step is known as a statement.
• Statements should end with a semicolon.
Example
II) COMMENTS
• Multi-Line Comments
To write a comment that stretches over more than one line, you use a multi-line comment,
starting with the /* characters and ending with the * / characters.
• Single-Line Comments
In a single-line comment, anything that follows the two forward slash characters // on that
line will not be processed by the JavaScript interpreter.
Single line comments are often used for short descriptions of what the code is doing.
• Once you have created variable ,you can assign a value to the variable.
• Variable name should describe the kind of data the variable holds.(ex.quantity)
• The equals (=) operator is used to assign a value to the variable.
• Until you have assigned a value to the variable ,the programmer say the value is
undefined.
Example:
Example:
Example:
Here are six rules you must always follow when giving a variable a name:
• The name must begin with a letter, dollar sign ($), or an underscore (_). It must not start
with a number.
• The name can contain letters, numbers, dollar sign ($), or an underscore (_). Note that
you must not use a dash(-) or a period (.) in a variable name.
• You cannot use keywords or reserved words.
• All variables are case sensitive, so score and Score would be different variable names.
• Use a name that describes the kind of information that the variable stores.
• If your variable name is made up of more than one word, use capital letter for the first
letter of every word after the first word.
In addition to these three data types, JavaScript also has others (arrays,objects,
undefined, and null)
CREATING AN ARRAY:
• The values are assigned to the array inside a pair of square brackets, and each value is
separated by a comma.
• The values in the array do not need to be the same data type, so you can store a string, a
number and a Boolean all in the same array.
• This technique for creating an array is known as an array literal.
Example-1:
var colors;
colors =['white', 'black', ' custom '];
VALUES IN ARRAYS:
• Values in an array are accessed as if they are in a numbered list. It is important to know
that the numbering of this list starts at zero (not one).
• Each item in an array is automatically given a number called an index. This can be used
to access specific items in the array.
• Consider the following array which holds three colors:
var colors;
colors= ['white ',
'black ',
' custom'];
• To retrieve the third item on the list, the array name is specified along with the index
number in square brackets.
Example:
var itemThree;
itemThree = colors [ 2] ;
• Here you can see a variable called i temThree is declared. Its value is set to be the third
color from the colors array.
• Each array has a property called length, which holds the number of items in the array.
• The name of the array is followed by a period symbol (or full stop) which is then
followed by the length keyword.
Example:
var numColors ;
numColors =colors.length;
• Above you can see that a variable called numColors is declared. Its value is set to be the
number of the items in the array.
V) EXPRESSIONS:
• An expression evaluates into (results in) a single value.
Broadly speaking there are two types of expressions.
• You can perform operations on any number of individual values (see next page) to
determine a single value.
For example:
var area = 3 * 2;
OPERATORS:
• Operators allow programmers to create a single value from one or more values.
• Assignment Operators
• Arithmetic Operators
• String Operators
• Comparison Operators
• Logical Operators
Assignment Operators:
• JavaScript contains the following mathematical operators, which you can use with
numbers.
STRING OPERATOR:
• There is just one string operator: the+ symbol. It is used to join the strings on either side of it
• To create a function, you give it a name and then written the statements needed to
achieve its tasks inside the curly braces. This is known as Function Declaration.
• Declare a function using the function keyword.
• Give the function name followed by parenthesis.
• write the block of code(statements) within curly braces.
• The wallOne variable holds the value 15,which was calculated by the calculateArea()
function.
• The wallTwo variable holds the value 40,which was calculated by the calculateArea()
function.
• This also demonstrates how the same function can be used to perform the same steps with
different values.
v) Getting a multiple values out of a function:
• Functions can return more than one value using an array.
Example:
This function calculates the area and volume of a box.
`
• The areaOne variable holds the area of a box that is 3X2.The area is the first value in
the sizes array.
• The volumeOne variable holds the volume of a box that is 3X2X3.The volume is the
second value in the sizes array.
vi)Function Expression:
• If you put a function where the interpreter would expect to see an expression, then it is
treated as an expression, and it is known as a function expression.
• In function expressions, the name is usually omitted. A function with no name is called
an anonymous function.
Example:
• To use the method, you can use the object name followed by the method name.
hotel.checkAvailability().
Accessing an object:
• Access the object properties and methods of an object using dot notation(.).
Example :
Example:
• When a function is created at the top level of a script (that is, not inside another object or
function), then it is in the global scope or global context.
• All global variables also become properties of the window object. so when a function is
in the global context, you can access global variables using the window object, as well
as its other properties.
Built-in objects:
• The three groups of built-in objects
1. Browser object model
2. Document object model
3. Global JavaScript object
• The global objects do not form a single model. They are group of individual
objects that relate to different parts of the JavaScript language.
Properties and methods of string:
Example:
If… else statement: The if…else statement checks a condition, if it is resolves to true the
first code block will be executed, if the condition resolves to false the second code block
is run instead.
Switch statement: A switch statement starts with a variable called the switch value. Each case
indicates a possible value for this variable and the code that should run if the variable matches
that value.
Loops:
• Loops checks a condition .if it returns true ,a code block will run . then the
condition will be checked again and if it still returns true, the code block will run
again.it repeats until the condition returns false.
• There are 3 common types of loops
For loop, While loop, Do while loop
For loop:
While loop:
Do while loop: