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

147
Views
How to add if statement in arrow function

i'm a beginner in JS and was wondering what is the syntax of an arrow function with an if statement with a regular function such as function

getStringLength(string){
  let stringLength;
  if (string.length === 1){
     stringLength = `La chaîne contient qu'un seul caractère`;
  } else {
     stringLength = `La chaîne contient ${string.length} caractères`;
  }
     return stringLength;
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

That would be

const getStringLength = (string) => {
  let stringLength;
    if (string.length === 1){
            stringLength = `La chaîne contient qu'un seul caractère`;
    } else {
            stringLength = `La chaîne contient ${string.length} caractères`;
    }
    return stringLength;
}
about 4 years ago · Juan Pablo Isaza Report

0

With ternary this would look like this using an arrow function.

Note that with arrow functions you can avoid using the return keyword

  
const getStringLength = (string) => string.length === 1 ? `La chaîne contient qu'un seul caractère` : `La chaîne contient ${string.length} caractères`

console.log(getStringLength('a'))
console.log(getStringLength('abcdef'))

about 4 years ago · Juan Pablo Isaza Report

0

You can also do it in one line

const getStringLength = (string) =>
  string.length === 1 ?
  `La chaîne contient qu'un seul caractère` :
  `La chaîne contient ${string.length} caractères`
  
  
console.log(getStringLength('a'))
console.log(getStringLength('ab'))
  

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!