JavaScript learning corner weekly updates (3.24.22)

Toni T Diep
2 min readMar 24, 2022

What bout JavaScript built-in every() function when dealing with arrays? I tend to confuse it with the has() method — so please do not do that. When in doubt always reference MDN Web Docs and W3Schools — at least that is my first Google attempt. If you are an audio kind of learner then there is Youtube, as another resource.

This Leetcode challenge surfaced further implementation to understand how to use every() with array and string manipulation in the approach I have solved it with. Give this problem a try and see what you encounter. I used the every() function here to check the “word” array’s string of words to find only common characters that are pushed into the “finalArray” emptied array = [] conditional into the forEach method iteration. Within the forEach method, I also implemented the map method with the replace() method to assess the string of words in the “word” array. According to w3school, the return value in replace() thru map() allows for searching a string for a value or regular expression. Here is my attempt on this Leetcode challenge.

I am excited to learn about every() method since we can easily confuse it with various JS built-in methods when dealing with arrays. According to w3schools the JS Array for every() method provides the following functions:

  • The every() method executes a function for each array element.
  • The every() method returns true if the function returns true for all elements.
  • The every() method returns false if the function returns false for one element.
  • The every() method does not execute the function for empty elements.
  • The every() method does not change the original array.
https://www.w3schools.com/jsref/jsref_every.asp

has() method “method returns a boolean indicating whether an element with the specified key exists or not. We can still do this upon initializing a new Map(). The argument in has() contains a key so in has(key). This key of the element is being tested as a boolean inquiry about the presence and its value in the Map object. Therefore, the return value is true only if the element of the specified key exists otherwise it is false. (MDN Web Docs).

//using has() referenced from MDN Web Docs 
let myMap = new Map()
myMap.set('bar', "foo")
myMap.has('bar') // returns true
myMap.has('baz') // returns false

Thank you for reading. Happy coding, we got this.

--

--

Toni T Diep

multilingual Software Engineer, always learning and growing.