Introduction to JavaScript Basics
Introduction to JavaScript Basics
Introduction
• JavaScript was introduced in 1995.
• Initially the language was used to add programs to web pages in the
Netscape Navigator Browser. Later adopted by all other major Web
browser.
• This language has made the web application capable of direct
interaction without a page reload for every action.
• JavaScript has almost nothing to do with the programming language
name Java. The similar name was inspired by marketing
consideration.
Features of JavaScript
• JavaScript is a full-fledges programming language that enables
dynamic interactivity on website on websites when applied to an
HTML document.
• All popular web browsers support JavaScript as they provide built-in
execution environments.
• It is a light-weighted and interpreted language.
• It is a case-sensitive language.
• It provides good control to the users over the web browser.
JavaScript code
• JavaScript code is inserted between <script> and </script> tag.
• This script tag can be placed in the <body>, or in the <head> section
of an HTML page, or in both.
JavaScript Example-2
<html>
<body>
<script>
document.write(“This is a JavaScript program inside body tag.”);
</script>
</body>
</html>
JavaScript Example-2
<html>
<head>
<script>
document.write(“This is a JavaScript program inside head tag.”);
</script>
</head>
<body>
</body>
</html>
JavaScript Comment
• The JavaScript comments are used to add information about the
code, warnings or suggestions to make the code easy to understand.
• The JavaScript comment is ignored by the JavaScript engine.
• Type of JavaScript Comments:
• Single-line Comment
• Multi-line Comment
Types of JavaScript Comments
Single line Comment Multi line Comment
• It is represented by double slashes (//). • It can be used to add single as well as
• It can be used before and after the multi line comments.
statement • It is represented by forward slash with
asterisk then asterisk with forward slash.
<script> <script>
//It is a single line comment /* It is a multi line comment
document.write(“Single line”); It will not be displayed */
</script> document.write(“Multi line”);
</script>
JavaScript Variables
• Variables are containers for storing data.
• There are some rules while declaring a JavaScript variable (also
known as identifiers).
• Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ )
sign.
• After first letter we can use digits (0 to 9), for example value1.
• JavaScript variables are case sensitive, for example x and X are different
variables.
JavaScript Variable Declaration and Initialization
• Declaration of a variable is used to specify the variable and type.
• Initialization is the process of assigning a value to the variable.
• Different ways of declaring any JavaScript Variable:
• var
• let
• const
• Even without declaring anything
Number • All JavaScript are stored as decimal numbers (double precision 64 bit floating //With decimals
point format (IEEE 754)). let num1 = 10.00;
//Without decimals
let num2 = 10;
BigInt • JavaScript BigInt is a new datatype that can be used to store integer values let x = BigInt("123456789012345678901234567890");
that are too big to be represented by a normal JavaScript number.
Boolean • Boolean can only have two values: true or false. let x = 5;
let y = 5;
(x == y) // Returns true
Undefined • undefined is a primitive value automatically assigned to variables that have let x;
just been declared. var y;
Symbol • Symbols are unique and it can be used as keys for objects. let a = symbol(‘sample’);
Null • Null declares a variable with an empty value and undefined defines a document.write(null == undefined) //true
variable with no value. document.write(null == undefined) //false
Datatypes cont.
• A composite or derived or compound data type is any data type
which can constructed using primitive data types and other
composite data types.
• Followings are the composite datatype in JavaScript:
• Object – An object contains different types of values.
• Array – It represents a collection of similar element in JavaScript.
• RegExp – It represents regular expression in JavaScript
JavaScript Operators
• There are following types of operators in JavaScript:
• Arithmetic Operators
• Comparison (Relational) Operators
• Bitwise Operators
• Logical Operators
• Assignment Operators
JavaScript Arithmetic Operator
Operator Description Example
+ Addition 10+20 = 30
- Subtraction 20-10 = 10
* Multiplication 10*20 = 200
** Exponentiation 10**2 = 100
/ Division 10/2 = 5
% Modulus 10%2 = 0
++ Increment Var a = 10
a++; // a is now 11
-- Decrement Var a = 10
a--; // a is now 9
JavaScript Comparison Operator
Operator Description Example
== Is equal to var x = 10;
x == ‘10’ // true
=== Identical (equal and of same type) var x = 10;
x === ‘10’ // false
!= Not equal to var x = 10;
x != ’10’ // false
!== Not identical var x = 10;
x !== ‘10’ // true
> Greater than 20 > 20 // false
>= Greater than or equal to 20 >= 20 // true
< Less than 20 < 20 // false
<= Less than or equal to 20 <= 20 // true
? Conditional operator Conditional Exp ? Exp If True : Exp If False
JavaScript Bitwise Operators
Statements
Condition
Input / Output
JavaScript Loops
• Loops is used to execute a block of code a number of times.
• Mainly there exists three types of loop:
• While loop
• Do-While loop
• For loop
While loop
• The JavaScript while statement creates a loop that executes a block of
statements as long as a condition evaluates to true.
• The while statement evaluates the expression before each iteration of
the loop.
• If the expression evaluates to true, the while statement executes the
block of statements. Otherwise, the while loop exists.
• Because the while loop evaluates the expression before each
iteration, it is known as a pretest loop.
• If the expression evaluates to false before the loop enters, the while
loop will never execute.
Syntax and Flowchart for While loop
• Syntax
while (expression)
{
// statement
}
Example of While loop
• Print all the number from 1 to 10. • Output:
1
let count = 1; 2
while( count <= 10) 3
{ 4
document.write(count); 5
count = count + 1; 6
} 7
8
9
10
Do-While loop
• The do-while loop statement creates a loop that executes a block of
statements until a condition evaluates to false.
• Unlike the while loop, the do-while loop always executes the block of
statements at least once before evaluating the expression.
• Because the do-while loop evaluates expression after each iteration,
it’s often referred to as a post-test loop.
Syntax and Flowchart for Do-While loop
• Syntax
do
{
// statement
}while(expression);
Example of Do-While loop
• Print all the number from 1 to 10. • Output:
1
let count = 1; 2
do 3
{ 4
document.write(count); 5
count = count + 1; 6
} while (count <= 10); 7
8
9
10
For loop
• The for loop statements creates a loop with three expressions. These are:
• Initializer
The for statement executes the initializer only once the loop starts. It initializes a
loop variable in the initializer.
• Condition
The condition is a Boolean expression that determines whether the for should
execute the next iteration.
The for statement evaluates the condition before each iteration. If the condition
is true (or is not present), it executes the next iteration. Otherwise, it’ll end the loop.
• Iterator
The for statement executes the iterator after each iteration.
Syntax and Flowchart of For loop
• Syntax