Function returns undefined when using in find(), but works when setting useState variable

I’m struggling with a strange issue. I have a select component that passes a value called "classID" up to a useState. I have an array mapped over the select component that looks like this (sent from NodeJS API):

[{classID: 371, teacherID: 1, subjectID: 3, subjectTitle: 'Mathematics', subjectLevel: 'IGCSE Level', …}, {classID: 370, teacherID: 1, subjectID: 2, subjectTitle: 'Physics', subjectLevel: 'IGCSE Level', …}]

Each object contains a "classID", "subjectID" etc. Here are the states:

  const [classID, setClassID] = useState("");
  const [subjectID, setSubjectID] = useState("");

The classID is set with a useState variable during onChange within the select component:

<CFormSelect className="pointer" onChange={setClass} value={classID}>
  <option value="">Select A Class</option>
  { => (
    <option key={c.classID} value={c.classID}>
      {c.subjectTitle} {c.subjectLevel} ({c.syllabusCode} - {c.classIdentifier})

Which feeds into this function:

const setClass = (event) => {
    let result = classOption.find(
      (item) => item.classID ===

What I am trying to do is use the "" (which is classID) to set the classID state variable, and then use it again to find the subjectID element within the object that has the classID === (as per the array shown above). I would then like to set "subjectID" to the subjectID element that the function returns.

The function, however, returns undefined. If I manually replace "" in the find() function with a hardcoded classID, the function returns the correct subjectID. As soon as I try to do the same with "", the function returns undefined. What’s strange is that returns with the correct value when I console.log

For some reason the find() function doesn’t accept it.

Please let me know if I can offer any other code/information. I’d greatly appreciate any assistance

>Solution :

use == rather than === for your code " item.classID === "

For plenty of information:
Which equals operator (== vs ===) should be used in JavaScript comparisons?

Leave a Reply