JavaScript Data Types

1- primitive datatype


// String

const name1 = "John Deo"; 
// Defined variable name1 and initialized value "John Dev".

console.log(name1); 
// ("run from",name1 )      

// Number

const num1 = 7;  
// Defined variable num1 and initialized 7 in num1 
        
const num2 = 3.1415;
// Defined variable num2 and initialized 3.1415 in num2
      
const num3 = 89e5;
// Defined variable num3 and initialized 89e5 in num3 
       
console.log(num1, num2, num3); 
// ("run from", num1, num2, num3)

// Boolean

const data1 = true;
// Defined data1 variable and initialized boolean value true  
  
const data2 = false;
//Defined data2 variable and initialized boolean value false   

console.log(data1, data2);
 // ("run from", data1, data2)

// Undefined

let undefine;  
// Defined undefine variable and left as is uninitialzed 
// without any value

console.log(undefine);
// ("run from", undefine)   

// Null

const n = null;
// Defined n variable (special keyword denoting a null value)  

console.log(n); 
// ("run from", n) 

// Symbol

let id = Symbol("nehal"); 
// Symbols are unique and cannot be changed. In JavaScript 
// ES6 it was introduced as a new primitive data type.

console.log(id);

Output


John Deo
7 3.1415 8900000
true false
undefined
null
Symbol(nehal)
2- Object datatype



// Array

const animal = ["cat", "dog", "goat"];
//  Defined animal variable and initialized 3 Array value

console.log(animal[1]); 
// ("run from animal[1] than print is index value 2(dog)
// and array value(1));

// Object;

const student = {    
// Defined student variable 
  
  Name: "Anil Kumar", 
// Defined object name  

  class: 6,   
// Defined object class      
    
  Roll: 45, 
// Defined object Roll            
};
console.log(student); 
// ("run from", student than print all object value)  

// Date

let now = Date();
// Defined now variable and initialized date and 
// Define value any date month year       

//alert=(now);
// ("run from", shows current date/time)

console.log(now);       

Output


dog
{ Name: 'Anil Kumar', class: 6, Roll: 45 }
Thu Jun 15 2023 13:04:55 GMT+0530 (India Standard Time)

How to use JavaScript String repeat()

Program to use JavaScript String repeat() method


let a = "Hello ";

let b = a.repeat(3);

console.log(b);

Output


Hello Hello Hello

Steps Description

  1. Declare two variables a and b
  2. Initialize two variable with value of string a = "Hello ".repeat(3) and b
  3. repeat() Method Whatever words or letters are written in the string, they can be repeated again and again.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript String repeat() method


let c = "Hello World \n";

let d = c.repeat(3);

console.log(d);

Output


Hello World 
Hello World 
Hello World 

Steps Description

  1. Declare two variables c and d
  2. Initialize two variable with value of string c = "Hello World \n".repeat(3) and d
  3. repeat() Method Whatever words or letters are written in the string, they can be repeated again and again.
  4. The value of d variable is displayed with the help of console.log inbuilt function

How to use multiply two positive numbers in C Language

Program to use multiply two positive numbers in C Language


#include <stdio.h>
int main()
{
    int a, b, i;
    int result = 0;
    printf("enter two number to be multiplied : ");
    scanf("%d%d", &a, &b);
    for (i = 1; i <= b; i++)
    {
        result = result + a;
    }
    printf("result  =   %d\n", result);
    return 0;
}

output


enter two number to be multiplied : 4
3
result = 12

How to use JavaScript String indexOf()

Program to use JavaScript String indexOf() method


let a = "JavaScript best programing language";

let b = a.indexOf("best");

console.log(b);

Output


11

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of string a = "JavaScript best programing language" and b
  3. indexOf() method will print the number of the index if the word or character is found in the string. will print -1 if not found
  4. The value of b variable is displayed with the help of console.log inbuilt function

How to use JavaScript String length

Program to use JavaScript String .length


let a = "Hello World in JavaScript";
let b = a.length;
console.log("String length =",b);

Output


String length = 25

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of string a = "Hello World in JavaScript" and b
  3. String .length Calculates the length of a string by counting its characters and output it as a number.
  4. The value of b variable is displayed with the help of console.log inbuilt function

How to use JavaScript String replace()

Program to use JavaScript String replace() method


let a = 'My Car is Blue';

console.log("data =", a);

let b = a.replace('Blue', 'Red');

console.log("replace data =",b);

Output


data = My Car is Blue
replace data = My Car is Red

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of string a = 'My Car is Blue' and b = a.replace('Blue', 'Red')
  3. replace() method if a word or character is found in the string after searching, then it works to replace that word or character with another word or character.
  4. The value of a variables and b variables is displayed with the help of console.log inbuilt function

Program to use JavaScript String replace() method


let c = 'My favorite Color Green laptop Color Green bag Color Green';

console.log("data =", c);


let d = c.replace(/Green/g, "Yellow");

console.log("replace data =",d);

Output


data = My favorite Color Green laptop Color Green bag Color Green
replace data = My favorite Color Yellow laptop Color Yellow bag Color Yellow

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable with value of string c = 'My favorite Color Green laptop Color Green bag Color Green' and d = c.replace(/Green/g, "Yellow")
  3. replace() method if a word or character is found in the string after searching, then it works to replace that word or character with another word or character.
  4. The value of c variables and d variables is displayed with the help of console.log inbuilt function