A taxi driver picks up 4 passengers from Jaipur station. The passengers were Hitesh Piyush Anirudh and Akash. Before the passengers came the taxi was empty.
let taxi=[];
After the passengers got into the taxi, the driver starts the journey.
let taxi =['hitesh','piyush','akash','anirudh']
Length Method
On their way they came across a toll, where they have to pay an amount to move forward. The toll manager asked the driver how many passengers were there in his taxi. The driver used the length method in JS to tell the number of passengers.
taxi.length // 4
Sort() Method
After travelling for 2 hours, they wanted to have some tea and snacks. So they took a stoppage at a tea cum snacks cafe. After the break, while getting back in the taxi the passengers had a little argument about who will sit at which seat. To resolve this issue the driver the sort() method to take them seat.
taxi.sort() // ['akash','anirudh','hitesh','piyush']
Pop() method
After travelling for further 1.5 hours Piyush reached his destionation which was Amber Fort. So he payed his fare and left.
taxi.pop() // ['akash','anirudh','hitesh']
Shift() method
After another 2 hours of journey, the taxi reached Hawa Mahal, where Akash got down.
taxi.shift() // ['anirudh','hitesh']
Push() method
On their way, the passenger got another passenger on the road named Manu who wanted to go to Jaipur Zoo. So the driver took him into the taxi using push method to take him at the last seat.
taxi.push('manu') // ['anirudh','hitesh','manu']
Unshift() method
The driver got another passenger on the way named Vinayak. The driver used Unshift() method to take him on the front seat.
taxi.unshift('vinayak')//['vinayak','anirudh','hitesh','manu']
At() method
During the journey, Vinayak forgot to tie his seat belt. So the traffic police stopped the taxi at signal and asked the driver, the name of the person at first position for which he uses at() method, which returns the value at given index.
taxi.at(0) // 'vinayak'
Concat() method
The taxi’s one of the tyre got punctured. So the passengers of taxi had to take bus, since it got late Hitesh let the other passengers stay at his home. So they got into the bus using the concat() method.
let bus=['tejas']
bus=bus.concat(taxi) //['tejas', 'vinayak','anirudh','hitesh','manu']
Join() Method
Before the passengers got into the bus, the driver kept the record of the last passengers who were in his taxi, for which he used the join() method.
taxi.join(',') // "vinayak,anirudh,hitesh,manu"
Slice() method
While in the bus, Hitesh called his home to inform his family that he is bringing guests at his house. His wife asked him how many people he is bringing including himself. For this Hitesh used Slice() method.
bus.slice(1)//['vinayak','anirudh','hitesh','manu']