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

124
Views
Javascript: How to add an optional forward slash to a template literal

So I am trying to have two optional arguments and an optional forward slash. The reason I need an optional forward slash is because the base url is just localhost:3000/web/reserve not localhost:3000/web/reserve/. The default empty arguments are working as expected when passing nothing into webpageCheck however I can't get the optional (/) to work. If I did http://localhost:3000/web/reserve/${label}${count} it would fail when calling webpageCheck because there is no forward slash at the end. Does anyone know how to do this?

const web = {
        label: 'Hello',
        count: '10',
    };
    
const webpageCheck = (label = '', count = '') => {
     const optionalSlash = '/'
      cy.url().should(
         'eq',
         `http://localhost:3000/web/reserve${optionalSlash + label}${optionalSlash + count}`
        );
    };
    
 webpageCheck()
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

So, it sounds like the optionalSlash should be in the string before label or count, and if label or count is missing, we skip adding the optionalSlash for that variable. If that's true, you could do a simple ternary when constructing the string.

`http://localhost:3000/web/reserve${
  label ? optionalSlash + label : ''
  }${
  count ? optionalSlash + count : ''}`
about 4 years ago · Juan Pablo Isaza Report

0

You could collect the parts in an array, filter it by boolean and join with wanted separator.

const
    web = { label: 'Hello', count: '10' },
    webpageCheck = (label = '', count = '', separator = '/') => 
        ['http://localhost:3000/web/reserve', label, count]
            .filter(Boolean)
            .join(separator);

console.log(webpageCheck());
console.log(webpageCheck(web.label));
console.log(webpageCheck(web.label, web.count));

about 4 years ago · Juan Pablo Isaza Report

0

You should try to define a string with your logic, something like this:

const web = {
        label: 'Hello',
        count: '10',
    };
    
const webpageCheck = (label = '', count = '') => {
     //Logic example
     let addressOptionsString=(label!==''? ("/" + label):'') + (count!==''? ("/" + count):'')
      cy.url().should(
         'eq',
         `http://localhost:3000/web/reserve${adressOptionsString}`
        );
    };
    
 webpageCheck()
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!