Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
if statment not working and getting undefined

I have a code which add a class on my navbar, this class will be added only on specific pages. I check which pages like this pathName= window.location.pathname.split('/'); this code returns me an array with the splited path, then I use the foreach loop to check if this page matches the requirements. If it does, the path will be undefined. But my if statment doesn't work because, I get this error Cannot read properties of undefined (reading '2')

The method where I'm getting a path

let pathName;
$(window).on('load', function () {
    if($('.owl-item').length<5){
    }
    pathName= window.location.pathname.split('/');
}

My scrolling method

$(window).scroll(function (evt) {
        if(pathName[2]===undefined) the line where I getting the error{
            **something**
            }else{
                **something**
            }
        }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have solved the problem I changed my if statment to this

Before

$(window).scroll(function (evt) {
        if(pathName[2]===undefined) **this** {
            **something**
            }else{
                **something**
            }
        }

After

$(window).scroll(function (evt) {
        if(typeof pathName[2]==='undefined') **to this**{
            **something**
            }else{
                **something**
            }
        }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!