Content

  • JavaScript Numbers and Methods
  • JavaScript Conditional Statements
  • JavaScript Switch Statement
  • Loops in JavaScript
  • JavaScript Maps
  • JavaScript Type Conversion

JavaScript Numbers and Methods

  • JavaScript has only one type of number,numbers can be written with or without decimals.
  • Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.
Example :
// A number with decimals
let x = 3.14;

// A number without decimals
let y = 3;
JavaScript Number Methods
  • Number methods help you work with numbers.
  • The toString() method returns a number as a string.
  • toFixed() returns a string, with the number written with a specified number of decimals
  • Number() can be used to convert JavaScript variables to numbers
  • parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned
  • NaN is a JavaScript reserved word indicating that a number is not a legal number.
Example : Number Methods
//toString()
let x = 123;
x.toString();

//toFixed()
let y = 9.656;
y.toFixed(2);

//Number()
Number("10");

//parseFloat()
parseFloat("10.33");

JavaScript Conditional Statements

  • Conditional statements are used to perform different actions based on different conditions
  • Very often when you write code, you want to perform different actions for different decisions
  • You can use conditional statements in your code to do this
  • if to specify a block of code to be executed, if a specified condition is true
  • else to specify a block of code to be executed, if the same condition is false
  • else if to specify a new condition to test, if the first condition is false
  • switch to specify many alternative blocks of code to be executed
Example : JavaScript Conditional Statements
//if..else
let age = 25;
if( age > 18)
{
    console.log("My age is greater than 18");
}
else
{
    console.log("My age is less than or 18");
}

//if else ladder
let age = 25;
if( age > 30)
{
    console.log("My age is greater than 30");
}
else if(age >21)
{
    console.log("My age is greater than 21");
}
else
{
    console.log("My age is less than 18");
}

//Switch case
switch (new Date().getDay()) 
{
    case 6:
      text = "Today is Saturday";
      break;
    case 0:
      text = "Today is Sunday";
      break;
    default:
      text = "Looking forward to the Weekend";
  }

Loops in JavaScript

Loops are used when we want to repeat an action or run some code multiple times
  • for Loop
  • for…in Loop
  • foreach Loop
  • while Loop
  • do…while Loop
for Loop
The for loop is used when we want to execute the set of statements for a certain number of times.
Example :
for(var j=1; j<=7; j++) 
{
console.log(j);
}
//check log in browser for output

for…in Loop
It is a special type of loop, used when we want to iterates over the properties of an object or the elements of an array.
Example :
var person = {name: "Shree", language: "JavaScript", age: 28};
for(var p in person) 
{  
console.log( p + " = " + person[p]); 
}

foreach Loop
With the help of forEach loop, we can execute a function on each item within an array.
Example :
const  evenno = [2,4,6,8,10];
evenno.forEach(showen);
 
function showen(item, index)
{
console.log(item);
}

while Loop
A while loop is used when we do not know how many times a certain block of code should execute.
Example :
let x = 1, y = 6;
while (x <= y) 
{
console.log(x);
x += 1;
}

do..While Loop
In this loop, the body inside the do statement is executed first,then the condition is evaluated.
Example :
let a = 1;
let b =3;
do {
console.log(a);
a++;
} while(a <= b);

JavaScript Maps

  • A Map holds key-value pairs where the keys can be any datatype.
  • A Map remembers the original insertion order of the keys.

Map Properties and Methods

MethodDescription
new Map()Creates a new Map
set()Sets the value for a key in a Map
get()Gets the value for a key in a Map
delete()Removes a Map element specified by the key
has()Returns true if a key exists in a Map
forEach()Calls a function for each key/value pair in a Map
entries()Returns an iterator with the [key, value] pairs in a Map
PropertyDescription
sizeReturns the number of elements in a Map
Example :
// Create a Map
const fruits = new Map();
                        
// Set Map Values
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);

fruits.get("apples");    // Returns 500
fruits.size; //result - 3

JavaScript Type Conversion

  • JavaScript variables can be converted to a new variable and another data type
  • Converting Strings to Numbers
  • Converting Numbers to Strings etc
Example
Number("3.14")    // returns 3.14
parseFloat("3.14") //returns 3.14

let x = 123;
x.toString(); // returns 123 as string