Get n numbers from the left using JavaScript

You have 2 options:

Option 1: transform into a string

javascript
const getNumbersFromLeft = (num, left) => {

    return parseInt(num.toString().slice(0, left))

}

getNumbersFromLeft(123456, 4) // 1234

Option 2: without transforming into a string

javascript
const getNumbersFromLeft = (num, left) => {

    let num_length = Math.floor(Math.log10(num)) + 1

    if(left > num_length)
        left = num_length

    return Math.floor(num/(10 ** (num_length - left)))

}

getNumbersFromLeft(123456, 4) // 1234

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.