- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6684 Articles for Javascript
![Nikhilesh Aleti](https://www.tutorialspoint.com/assets/profiles/519798/profile/60_1221559-1663324574.jpg)
2K+ Views
An Array in JavaScript is datatype which is used to store homogeneous elements. These elements are stored at contiguous memory locations. We can access each data element in the array with the help of index numbers. The index numbers will start from 0. Syntax Following is the syntax of creating a JavaScript array − const array_name = [item1, item2, ...]; The following is the simple declaration of array in JavaScript. Const Body = ['Eyes', 'Nose', 'Lips', 'Ears']; Finding the mid element of an array We are required to write a JavaScript program that finds the middlemost ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
125 Views
We are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index.For example: If the input array is −const input = [1, 3, 1, 3, 5, 7, 5, 4];OutputThen the output should be −const output = [2, 6, 7, 10, 4];// all the duplicate ones are summed to index 0// all the duplicate threes are summed to index 1 and so on.Therefore, let’s write the code for this function −ExampleThe code for this will be −const input = [1, 3, 1, 3, 5, 7, 5, ... Read More
![Nikhilesh Aleti](https://www.tutorialspoint.com/assets/profiles/519798/profile/60_1221559-1663324574.jpg)
2K+ Views
In this article the given task is to split array elements into n sized chunks in JavaScript. We have some approaches to perform the splitting operation on array, those will be discussed below. Let’s assume some simple input and output scenarios – Assume there is an array containing elements in it. Example, array = [10, 20, 30, 40] and let’s take the chunk size as 2. Input = [10, 20, 30, 40] Ouput = [[10, 20], [30, 40]] The elements will be divided into two chunks, the first argument specifies the starting index and the second argument specifies the ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
74 Views
We have two arrays of Numbers, and we are required to write a function intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.For example:If input is −arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate'];Then the output should be −['world', 'you'];Approach:Had the arrays been sorted, we could have used the two-pointer approach with initially both pointing to 0 the start of the respective array and we could have proceeded with increasing ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
117 Views
We have an array of numbers like this −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];We are required to write a JavaScript function that counts the consecutive groups of nonnegative (positives and 0) numbers in the array.Like here we have consecutive non negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from 9 to end of array forms the second group.So, for this array, the function should return 2.Therefore, let’s write the code for this function −ExampleThe code for this will be −const ... Read More
![Nikhilesh Aleti](https://www.tutorialspoint.com/assets/profiles/519798/profile/60_1221559-1663324574.jpg)
1K+ Views
An Array is known as collection of similar data elements which are stored at contiguous memory locations. Array is such a simple data structure where we can access each data element in the array with the help index numbers, which starts from 0. The following is the basic declaration of array in JavaScript − const mobiles = ["SAMSUNG", "ONEPLUS", "APPLE"]; Sum of two consecutive elements of an array We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
138 Views
We are required to write a JavaScript function that takes in a string representing a number. The function returns true if the number is pandigital, false otherwise.A pandigital number is a number that contains all digits (0-9) at least once.Therefore, let’s write the code for this function −ExampleThe code for this will be −const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => { let legend = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; for(let i = 0; i < numStr.length; i++){ if(!legend.includes(numStr[i])){ continue; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
98 Views
We are required to write a JavaScript function that takes in two strings; creates and returns a new string with first 2 words of first string, next two words of second string, then first, then second and so on.For example: If the strings are −const str1 = 'Hello world'; const str2 = 'How are you btw';Then the output should be −const output = 'HeHollw o arwoe rlyodu btw';Therefore, let’s write the code for this function −ExampleThe code for this will be −const str1 = 'Hello world'; const str2 = 'How are you btw'; const twiceJoin = (str1 = '', str2 ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
220 Views
We are required to write a JavaScript function that takes in a string and returns the character which makes second most appearances in the string.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Hello world, I have never seen such a beautiful weather in the world'; const secondFrequent = str => { const map = {}; for(let i = 0; i < str.length; i++){ map[str[i]] = (map[str[i]] || 0) + 1; }; const freqArr = Object.keys(map).map(el => [el, map[el]]); freqArr.sort((a, b) => b[1] - ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
103 Views
We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0.And every time, the function encounters a unique (non-duplicate) character it should increase the mapping count by 1 otherwise it should map the same number for duplicate characters.For example: If the string is −const str = 'heeeyyyy';Then the output should be −const output = [0, 1, 1, 1, 2, 2, 2, 2];Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'heeeyyyy'; const mapString = str => { const res = []; ... Read More