skip to Main Content

I’m new beginner in react-native and JavaScript here, i am using a if-else condition.

i want to check the first the condition is correct if it’s correct then it will works in a different different function.
Here is my codes:

const ab = true; 
let x;
if (ab === false){
    
// i want here to stop passing value of x;
//write a code so that x value can't be passed to console.log(x).

}
else if (ab=== true){
 x= [1,2,3]
}

console.log(x);

only when ab equals true, x value able to use.
anyone can help me to write a codes for if statement where it will stop to passing values when the condition is correct.

Thanks for your trying in advance!

2

Answers


  1. Chosen as BEST ANSWER

    Basically i wanted to try stop to pass value with a condition but later i tried only one condition and it was worked.

    suppose,

    const ab = true; 
    let x;
    if (ab=== true){
     x= [1,2,3]
    }
    
    console.log(x);
    

    so in there if ab is true only that time it will give x values otherwise automatically it will stop. Thanks


  2. I’m also new beginner in JavaScript.
    maybe you can do like this:

    let x 
    const ab = true
    if (ab === false) {
        // do what you want
        return
    } else if (ab === true) {
        x = [1, 2, 3]
    }
    console.log(x)

    or like this:

    let x 
    const ab = true
    if (ab === false) {
        // do what you want
    } else if (ab === true) {
        x = [1, 2, 3]
    }
    if (x != null && x != undefined) {
        console.log(x)
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search