maxime schoeni
maxime schoeni

Reputation: 2867

Why are some JavaScript functions given conjugated names and not others?

As a non-english developer, I often wonder why some javascript functions take a "s" and others don't.

Example:

Array.includes()
Element.contains()

I could figure it's because they are verbs conjugated in the third person. But in that case, why don't the following conjugate?

Array.reduce()
Array.find()
Array.map()

Is there some rule I'm unaware of, or is it just a question of what sounds better for the developer who first writes a library?

Upvotes: 3

Views: 79

Answers (1)

mstephen19
mstephen19

Reputation: 1926

As someone who was an English teacher, I'll firstly say that the main reason this is the case is because English kinda sucks. The grammar is weird, and no matter how many rules you memorize, you'll never speak it absolutely perfectly. Even us native speakers struggle with grammar on a daily basis.

To answer your question thoroughly:

  1. Methods such as includes() and contains() are statements that describe the array/value and return a "yes" or a "no" (a true or false) statement.

English:

My shopping list includes bacon. Is this true?

JavaScript:

const includesBacon = shoppingList.includes('bacon');

console.log(includesBacon); // -> boolean
  1. Methods such as reduce() and map() perform an action on the array/value. You are literally commanding the array to do something to itself.

English:

Please map out a new shopping list based on the old, but all bacon. Now!

JavaScript:

shoppingList.map(() => 'bacon')

Upvotes: 4

Related Questions