All About Arrays

 


Arrays In Javascript

An array is an object. Many values contain an array. So, the array is important in javascript.

Arrays Syntex- const=['value', 'value', 'value']

Also can be written- Const a=[7, "string", boollean]

Some example array codes-

                 let marks_class_12 = [91, 82, 63, 84, false, "Not Present"]

print this- console.log(marks_class_12)

individual track the number then code is:

console.log(marks_class_12[0])

console.log(marks_class_12[1])

console.log(marks_class_12[2])

console.log(marks_class_12[3])

console.log(marks_class_12[4])

console.log(marks_class_12[5])// index written rules. means position.

Here- 0, 1, 2, 3 array index that means position 

                          0-91

                          1-82

                          2-63

console.log(marks_class_12[6]) // Will be undefined because index 6 does not exist

 Anyone can define [6] then the code is 

 marks_class_12[6] = 89 // Adding a new value to the array

then do console.log 

## If you want to change on arrays any position- 

marks_class_12[0] = 96 // Changing the value of an array

then type code- console.log(marks_class_12)

Here Full Code -

let marks_class_12 = [91, 82, 63, 84, false, "Not Present"]

console.log(marks_class_12[0])

console.log(marks_class_12[1])

console.log(marks_class_12[2])

console.log(marks_class_12[3])

console.log(marks_class_12[4])

console.log(marks_class_12[5])

console.log(marks_class_12[6]) // Will be undefined because index 6 does not exist

console.log("The length of marks_class_12 is", marks_class_12.length)

marks_class_12[6] = 89 // Adding a new value to the array

marks_class_12[0] = 96 // Changing the value of an array

console.log(marks_class_12)

Now 2 topics are very important - 1. typeof

                                                      2. .length

 

 typeof means - how are arrays??- object or string or boolean

                 let marks_class_12 = [91, 82, 63, 84, false, "Not Present"]

                 console.log(typeof marks_class_12)

.lenth means - how arrays length? -

                 let marks_class_12 = [91, 82, 63, 84, false, "Not Present"]

                 console.log("The length of marks_class_12 is", marks_class_12.length)


Arrays Methods

 we use this method to use easily our coding journey. Arrays methods are-

 1. toString

 2. join

 3. Pop()

 4. Push()

 5. Shift

6. Unshift

7. Delete

8. Concat

9. Sort

10. Splice

11. Slice

12. Reverse

                   

          toString Method in Js

 Convert an array to string and coma to separate value.

The Code Is- 

  let num = [1, 2, 3, 34, 4]

  let b = num.toString() // b is now a string 

  return: console.log(b, typeof b)

             1, 2, 3, 34, 4 string

    

        Join Method in JS

     Join all the arrays using a separator

     The Code is- 

      let num = [1, 2, 3, 34, 4]

      let b=num.join(" and ")

      Return- console.log(b)

                1 and 2 and 3 and 34 and 4 

             

              pop() Method

     Remove the last element from a arrays 

     The Code Is-   let num = [1, 2, 3, 34, 4]

                          let a= num.pop()

      Return- console.log(num, a)

                 here- num: [1, 2, 3, 34]

                           a= 4      

   ## Update is original arrays

  ## Return the popped of values

  

        push() Method

    Add a new element at the end of the arrays

   The Code Is- let num = [4, 2, 4, 6, 8]

                          let d = num.push(7)

                         console.log(num, d)

           Here print - num: [4, 2, 4, 6, 8, 7]

                               d= 7

       

         ## Modified original arrays

         ## Return this value


       Shift() Method

  Removes an element from the start of the array

  The code is -let num = [4, 2, 4, 6, 8]

                    let a= num.shift()

                    console.log(num, a)

        Here - num- [ 2, 4, 6, 8]

                   a= 4

   ## shift is changed original arrays

  ## Return the values

  

    Unshift() Method 

Add an element to the beginning and return the length of the new array

    The code is- let num = [4, 2, 4, 6, 8]

                         let a= num.unshift(98)

                         console.log(num, a)

      Here- num: [98, 4, 2, 4, 6, 8]

                a= 5

                                        Total Code

let num = [4, 2, 4, 6, 8]

let a = num.toString() //sting method

console.log(a, typeof a)

let b = num.join(" and ") //join method

console.log(b, typeof b)

let c = num.pop()// pop method

console.log(num, c)

let d = num.push(7)

console.log(num, d)

let e= num.shift()

console.log(num, e)

let f= num.unshift(89)

console.log(num, f)


   Delete Method ()

 Delete is not a method. It’s an operator.

 Arrays element can be deleted using the delete operator

 Code is-

   let num = [4, 2, 4, 6, 8]

   delete num [0] //here []: means function and 0, 1, 2 : arrays index

Return: console.log (num)

           ##delete option use arrays does not change / arrays length


      Concat()

  Concat is an important arrays method. We use React.js, Next.js, JS, Vanila.js, and Apps.

  Concat just joins 2 or more arrays.

    Code is-

    let a = [ 1, 2];

    let b = [3, 4];

    let c = [5, 6];

    a.concat (b, c)

    return=[1,2,3,4,5,6]

  ##Return a new arrays

 ## Does not  the existing arrays

 let num2=[10, 11, 12]

 let newarrays=num.concat(num2)

 console.log(newarrays)


      Sort ()

1.            Help arrange  alphabetic order

2.            Modified the original arrays

    Code is-

    let num = [4, 2, 4, 6, 8]

    num.sort()

    console.log (num)

    We arrange it in ascending order and then use compare function

    let compare= (a, b)=>{

    return (a-b)

   }

   let num = [4, 2, 4, 6, 8]

   num.sort(compare)

   console.log (num)

 

     Splice () Method

     Splice can be used to add a new item to an array

    Code is –

    let num = [4, 2, 4, 6, 8]

    num.splice (2, 6, 8)

    console.log(num)

 

    Here splice method start

    Position to add, no element to remove, element to add

     Here is all code

     

   delete num[1] //delete operator

   console.log(num)

   let num2 = [10, 11, 12]

   let newarrays = num.concat(num2)

   console.log(newarrays)

   num.sort()

   console.log(num)


   let compare = (a, b) => {

    return (b - a)

  }

  let num3 = [7, 2, 4, 6, 8]

  num3.sort(compare)

  console.log(num3)


  num.splice(0, 6, 8)

  console.log(num)


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 








































































Comments