Format a phone number function Here is a function that formats a phone number and adds a country code prefix if it is not included in it.

So, as an in input value it could be, for example, '23581010' or '4923581010' and in output it would become via the formatNumber function '(02) 358 10 10'.

Is the positioning of the let assignment ok in this code ? If something else feels not ok can you tell me ?

import { formatNumber } from 'libphonenumber-js';

export const formatGermanPhoneNumber = phoneNumber =&gt; {
  if (!phoneNumber) {
    return '';
  }

  const phoneNumberString = `${phoneNumber}`;

  let prefix = '';
  if (!phoneNumberString.startsWith('49')) {
    prefix = '49';
  }

  return formatNumber(`+${prefix}${phoneNumberString}`, 'National');
};
I don't know of any coding standards or other reasons why you should have to move it higher. Generally it's just personal preference and whichever is more readable.

As for the fact that you'd need to create a variable unnecessarily in some cases, creating a variable like this uses very little memory so it shouldn't be an issue unless the function is being looped over thousands of times. 

You could also consider another option, which is to use a ternary rather than mutating a variable:
let prefix = phoneNumberString.startsWith('49') ? '49' : ''