Skip to content
Advertisement

returning a variable of an if statement javascript

hi i want to use a variable that i create inside a else statement to use in another function but i cant figure out how

the code >

if (!e){
               await knex('empresa').insert({
                   razao_social,
                   cnpj
               }) 
            }else{
                const e_id = e.id
                console.log(e_id)
                return
            }

i want to get that e_id variable to use in another function but i dont seem to be able to do it

Advertisement

Answer

declare the variable in the global scope. And only populate it inside the if statement.

let e_id;

if (!e){
               await knex('empresa').insert({
                   razao_social,
                   cnpj
               }) 
            }else{
                e_id = e.id
                console.log(e_id)
                return
            }
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement