How does a For loop work in JavaScript

For Loop

The for loop is frequently used, usually where the loop will be traversed a fixed number of times.

Syntax:

Nested For Loop

For loop inside for loop

Syntax:

While loop

The while loop keeps repeating an action until an associated condition returns false.

Syntax:

Nested While loop

While Loop inside While Loop

Do While Loop

The do while loop is similar to while loop, but the condition is checked after the loop body is executed. This ensures that the loop body is run at least once.

Syntax :

Nested Do While Loop

Do while inside Do while

Tagged : / / /

What is the IF statement And Switch Statement in JavaScript?

If Statement

It is used to execute an instruction or block of instructions only if a condition is fulfilled.

Syntax: –

if (condition)
      {
           block of statements;
      }

If statement with logical operator

if ( (condition1) && (condition2) )
     {
        block of statements;
     }

If else Statement

if… else statement is used when a different sequence of instructions is to be executed depending
on the logical value(True/False) of the condition evaluated.

Syntax: –

if(condition)
   {
     Statement_1_Block;
   }
else
   {
     Statement_2_Block;
   }
   Statement_3;

Else If Statement

To show a multi-way decision based on several conditions, we use else if statement.

Syntax: –

If(condition_1)
   {
     statements_1_Block;
   }
else if(condition_2)
   {
     statement_2_Blocks;
   }
else if(condition_n)
   {
     Statements_n_Block;
   }
else
 statements_x;

Switch Statement

Check several possible constant values for an expression.

Syntax: –

switch(expression){
      case expression 1:
            block of statements 1;
            break;
      case expression2;:
            block of statements 2;
            break;
            .
            .
      default:
            default block of instructions;
      }

All the cases will be evaluated if you don’t use a break statement.

switch(expression){
            case expression 1:
                 block of statements 1;
                 break;
            case expression 2:
            case expression 3:
                 block of statements 2;
                 break;
            default:
                 default block of instructions;
            }

Tagged : / / /

Getting input from users in JavaScript

prompt( ) – The browser provides a built-in function that can be used to get input
from the user, named prompt. The prompt( ) method displays a dialog box that prompts
the visitor for input.

Once the prompt function obtains input from the user, it returns that input.

Syntax: – prompt(text, defaultText)

Ex:- prompt(“Enter Your Name: “, “name”);
prompt(“Enter Your Roll No. : “);

Good Approach

  • Inline
  • External

Inline

External

Good Approach

  • Inside head Tag
  • Inside body Tag

Inside head Tag

Inside body Tag

Tagged : / / /

JavaScript Operators

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators

Arithmetic Operators

Arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.

OperatorsMeaningExampleResult
+Addition4+26
Subtraction4-22
*Multiplication4*28
/Division4/22
%Modulus operator to get
the remainder in integer division
5%21
++IncrementA = 10;
A+ +
11
DecrementA = 10;
A− −
9

Relational Operators

OperatorsMeaningExampleResult
<Less than5<2False
>Greater than5>2True
<=Less than or equal to5<=2False
>=Greater than or equal to5>=2True
=Equal to5==2False
!=Not equal to5! =2True
===Equal value and same type5 === 5
5 === “5”
True
False
!==Not Equal Value or Not
same type
5 ! == 5
5 ! == “5”
False
True

Logical Operators

OperatorsMeaningExampleResult
&&Logical and(5<2)&&(5>3)False
||Logical or(5<2)||(5>3)True
!Logical not!(5<2)True

&&

Operand 1 Operand 2Result
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

||

Operand 1 Operand 2Result
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

!

OperandResult
FalseTrue
TrueFalse

Bitwise Operators

OperatorMeaning
<<Shifts the bits to left
>>Shifts the bits to the right
~Bitwise inversion (one‟s complement)
&Bitwise logical AND
|Bitwise logical OR
^Bitwise exclusive or

Bitwise logical AND &

Operand 1Operand 2Result (operand1&operand2)
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
Operand 1Operand 2Result (operand1&operand2)
111
100
010
000

Bitwise logical OR |

Operand 1Operand 2Result (operand1|operand2)
True 1True 1True 1
True 1False 0True 1
False 0True 1True 1
False 0False 0False 0

Bitwise XOR ^

Operand 1Operand 2Result (operand1^operand2)
True 1True 1False 0
True 1False 0True 1
False 0True 1True 1
False 0False 0False 0

Bitwise NOT ~

OperandResult
True 1False 0
False 0True 1

Assignment Operators

OperatorExampleEquivalent Expression
=𝑚 = 10𝑚 = 10
+=𝑚 += 10𝑚 = 𝑚 + 10
−=𝑚 −= 10𝑚 = 𝑚 − 10
∗=𝑚 ∗= 10𝑚 = 𝑚 ∗ 10
/ =𝑚 / =𝑚 = 𝑚/10
% =𝑚 % = 10𝑚 = 𝑚%10
<<=𝑎 <<= 𝑏𝑎 = 𝑎 << 𝑏
>>=𝑎 >>= 𝑏𝑎 = 𝑎 >> 𝑏
>>>=𝑎 >>>= 𝑏𝑎 = 𝑎 >>> 𝑏
& =𝑎 & = 𝑏𝑎 = 𝑎 & 𝑏
^ =𝑎 ^ = 𝑏𝑎 = 𝑎 ^ 𝑏
| =𝑎 | = 𝑏𝑎 = 𝑎 | b
Tagged : / /

Comments in JavaScript

Two types of Comment

  • Single Line Comment
  • Multi Line Comment

Single Line Comment

Single line comments start with //.
Text between // and the end of the line will be ignored by JavaScript.

// you can assign any type of value
var imvalue = 101;

var imvalue = 101; // assign any type of value

Multi Line Comment

Multi-line comments start with /* and end with /.

Any text between / and */ will be ignored by JavaScript.

Ex: –
/* Comment Here */

Adding // in front of a code line changes the code lines from an executable line to a comment.

var imvalue = 101;
// var imvalue = 101;

Tagged : / /

Initializing Variables in Javascript

Types of Initializing Variable

var roll;
roll = 101; var roll = 101; roll = 101;

var name;
name = “devopsschool.com”; var name = “devopsschool.com”; name = “devopsschool.com”;

price = 125.36;
Initializing Variable var price = 125.36; price = 125.36;

Whatever happens, is optional so only you roll = 101; can also write. Var is not necessary but if you use this is a good practice.

Rules of Initializing Variable

  • Strings are written inside double or single quotes.
  • Numbers are written without quotes.
  • If you put a number in quotes, it will be treated as a text string

Initializing Variable

var ans = true;
var result = false;

Initializing Variable example

var x = 10, y = 20, c = 30;
var fname = “DevOps”, lname = “School”;
var name = “DevOpsSchool”, roll = 101;
var name = “DevOpsSchool”,
roll = 101,
address = “Steel City”;
Tagged : / /

Declaring Variable and Re-declaring Variables in JavaScript

Declaring Variable

Declaring Variable Rule

  • Variable can contain a combination of letters, digits, underscores ( _ ), and
  • dollar signs ( $ ).
  • Must begin with a letter A-Z or a-z or underscore or dollar sign
  • A variable name cannot start with a number
  • Must not contain any space characters
  • JavaScript is case-sensitive
  • Can‟t use reserved keywords

Declaring Variable

A variable declared without a value will have the value undefined

Tagged : / /

List of keywords and reserved words in JavaScript

Keyword or Reserved words

vardeleteforletbrack
supervoidcasedostatic
functionnewswitchwhileinterface
catchelseifpackagefinally
thiswithclassenumdefault
implementsprivatethrowtiledtypeof
constexportimportprotectedreturn
truecontinueextendsin
instanceof
publictrydebuggerfalse

In JavaScript, we do not need to specify the type of the variable because it is dynamically used by the JavaScript engine.
We can use var data type. It can hold any type of data like String, Number, Boolean, etc.

Primitive Data Type Non-Primitive Data type

String Object
Number Array
Boolean RegExp
Undefine

Tagged : / / / /

Variables in Javascript with Rules

Variables

A variable is an identifier or a name which is used to refer a value. A variable is written with a combination of letters, numbers and special characters _ (underscore) and $ (dollar) with the first letter being an alphabet.

Ex-

c, fact, b33, total_amount etc.

Rules

• Variable can contain combination of letters, digits, underscores ( _ ), and
dollar signs ( $ ).
• Must begin with a letter A-Z or a-z or underscore or dollar sign
• A variable name cannot start with a number
• Must not contain any space characters
• JavaScript is case-sensitive
• Can‟t use reserved keywords

Tagged : / / / /

Identifiers and Reserved Words – JavaScript

An identifier is a name having a few letters, numbers and special characters _ (underscore). It is used to identify a variable, function, symbolic constant and so on.

Ex-

  • X2
  • PI
  • Sigma
  • matadd

Tagged : / / /