'why can't I write if else inside onsubmit handler?

I want to write some condition inside an onsubmit handler. But whenever I write if-else inside it, It doesn't work. After commenting out the condition everything works perfectly.

    const handleRegister = event => {
        event.preventDefault();
        const name = event.target.name.value;
        const email = event.target.email.value;
        const password = event.target.password.value;
        const confirmPassword = event.target.confirmPassword.value;
        console.log(confirmPassword);
        **if(password === confirmPassword){
            createUserWithEmailAndPassword(email, password);
        }
        else{
            notMatched = <span className='text-danger'>Password does not matched</span>;
        }**
    }

I write it for this form

<form onSubmit={handleRegister} className='text-start'>
                    <label>Name</label><br />
                    <input className='w-100 mb-3' type="text" name="name" id="" placeholder='Your Name' /><br />
                    <label>Email</label><br />
                    <input className='w-100 mb-3' type="email" name="email" id="" placeholder='Your Email' /><br />
                    <label>Password</label><br />
                    <input className='w-100 mb-3' type="password" name="password" id="" placeholder='Your Password' /><br />
                    <label>Confirm Password</label><br />
                    <input className='w-100 mb-3' type="password" name="confirmPassword" id="" placeholder='Confirm Password' /><br />
                    {errorMessage}{notMatched}
                    <input className='mb-3' type="submit" value="REGISTER" />
                </form>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source