Sharing is caring!

Javascript and basics (Javascript alert, array methods)

JavaScript is a popular programming language used for creating interactive web applications. It is a high-level, dynamic, and interpreted language that can be run on almost any modern web browser without the need for any additional plugins or software.

JavaScript array methods

  1. push() – adds one or more elements to the end of an array and returns the new length of the array.
  2. pop() – removes the last element from an array and returns it.
  3. shift() – removes the first element from an array and returns it, shifting all other elements down by one.
  4. unshift() – adds one or more elements to the beginning of an array and returns the new length of the array.
  5. slice() – returns a new array that contains a copy of a portion of the original array.
  6. splice() – removes or replaces one or more elements from an array at a specified index.
  7. concat() – combines two or more arrays into a new array.
  8. reverse() – reverses the order of the elements in an array.
  9. sort() – sorts the elements in an array.
let myArray = [1, 2, 3, 4];

myArray.push(5); // add 5 to the end of the array

let lastElement = myArray.pop(); // remove and return the last element of the array (5)

let firstElement = myArray.shift(); // remove and return the first element of the array (1)

myArray.unshift(0); // add 0 to the beginning of the array

let newArray = myArray.slice(1, 3); // create a new array with elements 2 and 3

myArray.splice(2, 1, 5); // remove one element at index 2 and add 5 in its place

let combinedArray = myArray.concat([6, 7]); // create a new array by combining myArray with [6, 7]

myArray.reverse(); // reverse the order of the elements in the array

myArray.sort(); // sort the elements in the array in ascending order

Javascript array

Add elements to array

To add an element to an array in JavaScript, you can use the push() method. Here is an example:

let myArray = [1, 2, 3, 4]; // original array

myArray.push(5); // add 5 to the end of the array

console.log(myArray); // [1, 2, 3, 4, 5]

The push() method adds the specified element to the end of the array and returns the new length of the array. You can also add multiple elements to an array using the push() method by passing in multiple arguments separated by commas. For example:

The push method

let myArray = [1, 2, 3, 4]; // original array

myArray.push(5, 6, 7); // add multiple elements to the end of the array

console.log(myArray); // [1, 2, 3, 4, 5, 6, 7]

Concat method

Alternatively, you can use the concat() method to add one or more elements to an array and return a new array. For example:

let myArray = [1, 2, 3, 4]; // original array

let newArray = myArray.concat(5); // add 5 to the end of the array and create a new array

console.log(newArray); // [1, 2, 3, 4, 5]

Javascript alert

In JavaScript, the alert() function is used to display an alert dialog box with a message and an OK button. Here is an example:

alert("Hello, world!"); // display an alert dialog box with the message "Hello, world!"

When this line of code is executed, an alert dialog box will appear on the user’s screen with the message “Hello, world!” and an OK button. The user can click the OK button to close the dialog box.

Please leave a comment and share the blog if you liked it and it helps you. Thank you in advance.


1 Comment

Nikolaev_ztpi · January 31, 2024 at 2:17 am

thank you so much bro

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

Cheap flights with cashback