JavaScript - Internet Technologies

What is Internet Programming Language - javascript?

JavaScript is an object-oriented programming language which is lightweight and interpreted, used in building interactivity otherwise static HTML pages.

JavaScript code is not compiled but translated by the translator. This translator is embedded into the browser and is responsible for translating javascript code.

Key Points

  • It is interpreted, lightweight programming language.
  • Designed to create network-centric applications.
  • It is integrated with Java and HTML as a complementary.
  • It is referred to as open and cross-platform

JavaScript Statements

Those are the commands to the browser to tell what action to perform and are separated by semicolon (;).

Browser translates the JavaScript code line by line from the JavaScript statement.

Example of JavaScript statement:

Below table depicts the various JavaScript Statements −

Sr.No. Statement Description
1. switch case A block of statements in which execution of code depends upon different cases. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, adefaultcondition will be used.
2. If else Theifstatement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.
3. While The purpose of a while loop is to execute a statement or code block repeatedly as long as expression is true. Once expression becomes false, the loop will be exited.
4. do while Block of statements that are executed at least once and continues to be executed while condition is true.
5. for Same as while but initialization, condition and increment/decrement is done in the same line.
6. for in This loop is used to loop through an object's properties.
7. continue The continue statement tells the interpreter to immediately start the next iteration of the loop and skip remaining code block.
8. break The break statement is used to exit a loop early, breaking out of the enclosing curly braces.
9. function A function is a group of reusable code which can be called anywhere in your programme. The keyword function is used to declare a function.
10. return Return statement is used to return a value from a function.
11. var Used to declare a variable.
12. try A block of statements on which error handling is implemented.
13. catch A block of statements that are executed when an error occur.
14. throw Used to throw an error.

JavaScript Comments

  • Both C-style and C++ style are supported by JavaScript, thus:
  • JavaScript ignores the comments. Text between a // and the end of the line is considered as a comment.
  • Text between the characters /* and */ is treated as a comment having multiple lines.
  • Similar to //, opening sequence <!—of HTML also recognized by JavaScript for single-line comment.
  • The closing sequence --> of HTML comment is not recognized by JavaScript so it should be written as //-->.

Example

JavaScript variable

Variables store the information and are referred as containers.

Rules to declare variable in JavaScript

Below rules should be followed when declaring a variable in JavaScript.

  • Variable names are case sensitive i.e. a is different from A.
  • Variable name can only be started with an underscore (_) or a letter from a to z or A to Z, or dollar ( ‘$’ ) sign.
  • Numbers are used after a letter.
  • Variable names should not posses other special character.
  • Variable should be declared with var keyword before using it in program as follows-.

Variables can be initialized at any time as below-

Javascript Data Type

Below are the two kinds of data types -

  • Primitive Data Type
  • Non Primitive Data Type

Below table explains Primitive Data Types available in javaScript

Sr.No. Datatype Description
1. String

Can contain groups of character as single value. It is represented in double quotes.E.g. var x= “tutorial”.

2. Numbers

Contains the numbers with or without decimal. E.g. var x=44, y=44.56;

3. Booleans

Contain only two values either true or false. E.g. var x=true, y= false.

4. Undefined

Variable with no value is called Undefined. E.g. var x;

5. Null

If we assign null to a variable, it becomes empty. E.g. var x=null;

Below table explains Non-Primitive Data Types in javaScript

Sr.No. Datatype Description
1. Array
Can contain groups of values of same type. E.g. var x={1,2,3,55};
2. Objects
Objects are stored in property and value pair. E.g. var rectangle = { length: 5, breadth: 3};

JavaScript Functions

Group of reusable statements collectively is known as Function, which are used anywhere in a program. Keyword function is used in declaring or defining a function.

Key Points

  • Define a function by using function keyword followed by functionname, followed by parentheses ().
  • In parenthesis, parameters or attributes are defined.
  • Curly braces {} is used to enclose the reusable statements. This code is executed whenever the function is called.

Syntax

JavaScript Operators

Operators are used in performing operations on one, two or more operands. They are represented with symbols such as +, =, *, % etc. Below operators are supported by javascript −

  • Arithmetic Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Assignment Operators
  • Conditional (or ternary) Operators
  • Arithmetic Operators

Arithmatic Operators

Below table displays the arithmetic operators that are supported by javascript −

Operator Description Example
+ Add two operands. 10 + 10 will give 20
- Subtract second operand from the first. 10 – 10 will give 0
* Multiply two operands. 10 * 30 will give 300
/ Divide numerator by denominator 10/10 will give 1
% It is called modulus operator and gives remainder of the division. 10 % 10 will give 0
++ Increment operator, increases integer value by one 10 ++ will give 11
-- Decrement operator, decreases integer value by one 10 – will give 9

Comparison Operators

Below table displays comparison operators that are supported by javascript −

Operator Description Example
== Checks if values of two operands are equal or not, If yes then condition becomes true. 10 == 10 will give true
!= Not Equal to operator
Checks if the value of two operands is equal or not, if values are not equal then condition becomes true.
10 !=10 will give false
> Greater Than operator
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
20 > 10 will give true
< Less than operator
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
10 < 20 will give true
>= Greater than or equal to operator
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
10 >=20 will give false
<= Less than or equal to operator
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
10 <=20 will give true.

Logical Operators

Below table displays logical operators that are supported by javascript −

Operator Description Example
&& LogicalANDoperator returns true if both operands are non zero. 10 && 10 will give true.
|| LogicalORoperator returns true If any of the operand is non zero 10 || 0 will give true.
! LogicalNOToperator complements the logical state of its operand. ! (10 && 10) will give false.

Assignment Operators

Below table displays all the assignment operators supported by javascript −

Operator Description Example
= Simple Assignment operator
Assigns values from right side operands to left side operand.
C = A + B will assign value of A + B into C
+= Add AND assignment operator
It adds right operand to the left operand and assign the result to left operand
C += A is equivalent to C = C + A
-= Subtract AND assignment operator
It subtracts right operand from the left operand and assign the result to left operand
C -= A is equivalent to C = C - A
*= Multiply AND assignment operator
It multiplies right operand with the left operand and assign the result to left operand
C *= A is equivalent to C = C * A
/= Divide AND assignment operator
It divides left operand with the right operand and assign the result to left operand
C /= A is equivalent to C = C / A
%= Modulus AND assignment operator
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
C %= A is equivalent to C = C % A

Conditional Operator

It is termed as ternary operator, as it has three operands.

Operator Description Example
?: Conditional Expression If Condition is true? Then value X : Otherwise value Y

Control Structure

These control the execution flow of a program. Below are the various control structure that JavaScript supports.

  • if … else
  • switch case
  • do while loop
  • while loop
  • for loop

If … else

It is the fundamental control statement that is used to make decisions and execute statements conditionally.

Syntax

Example

Switch case

An expression is given to evaluate and several statement to be executed based on the value of the expression. The value of the expression is checked against each case until a match is found. If no match found, a default condition is used.

Syntax

Example

Do while Loop

The do…while loop is same as while loop except that the condition is checked at the end of the loop which means the loop is executed at least once even if the condition is false.

Syntax

Example

This will throw below result −

While Loop

The while loop is executed as long as the expression is true. If the value of the expression resulted false, the loop will be exited.

Syntax

Example

This will throw below output -

For Loop

  • It is considered to be the compact form of looping and consists below three important parts -
  • The loop initialization, where we initialize the counter to have a starting value. The initialization statement is executed even before the loop starts.
  • The test statement, where the given condition is tested to be true or false. If condition is true then code given inside the loop will be executed otherwise loop is exited.
  • The iteration statement where the counter is increased or decreased.

Syntax

Example

This will produce below output which is similar to while loop −

Creating Sample Program

Below is the sample program that displays time, when we click on button.

Output

internet-js_sample_program

« Previous Topics
Css
Html
Firewall Security
Next Topics »
Php

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Internet Technologies Topics