Stack Overflow is the most popular q&A platform for programmers. If a question is popular on Stack Overflow, you’ve probably already encountered or will encounter the same type of question.

I’ve rounded up five of the most popular questions on Stack Overflow that I hope will help you.

Copyright Notice: The content on Stack Overflow is distributed based on CC BY-SA.

TOP1: How do I remove a particular element from an array?

  • Read number: 8 million
  • Number of endorsements: 9,304
  • Number of collections: 1423

In fact, the question itself has a certain ambiguity. When we talk about removing a particular element, there are three ways to think about it:

  • Deletes an element at a specific location
  • Removes an element of a particular value
  • Removes all elements with a specific value

Here are three cases to discuss.

Deletes an element at a specific location

If we need to delete elements at a location, then things are very simple. The array API has a method called array.splice that helps remove elements of a particular index.

The splice() method changes the contents of an array by removing or replacing existing elements

This method can take two arguments:

let arrDeletedItems = arr.splice(start, deleteCount)
Copy the code
  • Start: Deletes elements from this index location
  • DeleteCount: indicates the number of elements to be deleted

Example:

Here’s what an animation would look like:

With this method in hand, the previous requirement is very simple: if we want to delete elements at a particular location, we simply set the splice method deleteCount to 1.

For example, if we want to delete an element with an array index of 2 (true), we can write:

A simple test:

What is the result of the following expression?

[1, 2, 3].splice(2, 1)
Copy the code

Removes a specific value from the array

If we want to delete a particular value from the array, we do just two simple steps:

1. Find the index of the value in the array

2. Delete elements by index

The method used to find the indexOf an element in an Array is array.prototype.indexof () :

The **indexOf()** method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Once again, suppose we want to delete the element with the value 5, we just do this:

let arr = [1, 'a', true, 5, 'a', 3]let index = arr.indexOf(5)if (index > -1) {
  arr.splice(index, 1)
}
console.log(arr)
Copy the code

Removes all elements with a specific value

We just explained how to delete an element. Now if we need to delete all the elements of the array with a particular value, we just need to repeat the above steps a few times.

function removeAll(array, value){ for (let i = array.length - 1; i >= 0; i--) { if (array[i] === value) { array.splice(i, 1); }}}Copy the code

In the above function, we iterate through the array of elements from back to front, and if we find that the current element is equal to a specific value, we delete it using the splice method.

Example:

Test:

Why do we go backwards instead of forwards?

TOP2: How to tell if a DOM element is hidden in JavaScript?

  • Views: 2.8 million
  • Number of endorsements: 8063
  • Collection number: 920

The original problem limits us to judge through jQuery. But given that jQuery isn’t that popular anymore, I’m extending this to use native JavaScript.

There are two things you need to know to solve this problem:

  1. How do I hide an element in CSS
  2. How do I read and manipulate CSS properties in JavaScript

Let’s solve them one by one.

Hide an element in CSS

The property that controls the visibility of an element in CSS is visibility

There are two common values for this property:

visible

Default value. The element is visible

hidden

The element is hidden (but still takes up space)

Suppose our HTML file looks like this:

<head>    <style>        #stalker {            visibility: hidden;        }    </style></head><body>    <ul>        <li>bytefish</li>        <li id="stalker">a stalker</li>        <li>a dog</li>    </ul></body>
Copy the code

Then

  • a stalker
  • the node will be hidden in the page.

    Here we can debug from the browser console:

    Read and manipulate CSS properties in JavaScript

    If we want to determine if a DOM element is hidden in the page, we can simply check the CSS property in JavaScript.

    The check method is as follows:

    • The browser provides the window.getComputedStyle function to get all the attributes of a DOM element.
    • Get the value of a specific style using the style.getPropertyValue method.

    Of course, in addition to reading a CSS property, we can also modify the CSS property through JS. Modifying CSS properties is simple:

    TOP3: What is the use of Strict mode?

    • Number of views: 1.1 million
    • Number of endorsements: 7818
    • Collection number: 1530

    JavaScript is a very flexible language to start with, but too much freedom has led to a certain amount of confusion.

    Strict mode was introduced in ES5 to encourage people to abandon lax code styles. In this mode, many of the easily misunderstood notations are disabled.

    Strict mode is the first line of the script, or the first line of the function, with the string “Use strict”. This string is processed specially by the JS engine and turned on in strict mode.

    Without further ado, here directly on the code screenshot.

    1. In strict mode, undeclared variables are not allowed:

    2. In strict mode, variables or objects cannot be deleted

    3. Octal is not allowed in strict mode

    4. In strict mode, prohibit this keyword from pointing to global objects.

    In this case, this points to a global object, so the console prints Windows.

    In the case above, we use strict mode where this is prohibited from pointing to global objects, so the console prints undefined.

    Of course, there are many rules for strict mode, which I won’t go through here, but you can refer to this link if you are interested:

    www.runoob.com/js/js-stric…

    TOP4: How to jump to other pages?

    • Number of views: 6.7 million
    • Number of pros: 7,713
    • Collection number: 2,242

    The problem is relatively simple: location.href and location.replace can both do page jumps.

    location.href = 'https://bytefish.medium.com'location.replace('https://bytefish.medium.com')
    Copy the code

    How to determine if a string contains another string?

    • Views: 6.6 million
    • Number of pros: 7420
    • Collection number: 1501

    Like arrays, strings have an indexOf method:

    var string = "foo"; var substring = "oo"; console.log(string.indexOf(substring) ! = = 1);Copy the code

    After ES6, we can also use the includes method:

    const string = "foo";
    const substring = "oo";
    
    console.log(string.includes(substring));
    Copy the code

    summary

    These are the top 5 most popular JavaScript questions of all time under the JavaScript TAB on Stack Overflow. Have you ever run into this type of problem?

    Medium

    The English version of this article is published on Medium. We welcome all gold diggers to watch and clap on Medium.

    Top 5 Most Popular JavaScript Questions on Stack Overflow

    Remember to click from my link above to avoid the limits of the Medium paywall.