2D flip difference

I am making a platformer and I was trying to make a flip script myself which looks like this:

        if (horizontal > 0)
        {
            transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, transform.localScale.z);
        } else if (horizontal < 0)
        {
            transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
        }

But when I pressed the left arrow the player started flipping very fast from right to left. I still don’t know why. But I searched for another script and I found this:

        Vector3 characterScale = transform.localScale;

        if (horizontal < 0)
        {
            characterScale.x = -1;
        }

        if (horizontal > 0)
        {
            characterScale.x = 1;
        }

        transform.localScale = characterScale;

This script works fine. What is the difference between those scripts, why does my version goes crazy and the second one works fine?

Assuming the above code to run in Update, each frame you check if horizontal < 0, and if it is you negate the current localScale.x value. You then repeat this the next frame, and the one after, and so on. So it constantly flips between positive and negative values.

It sets a fixed value, -1 for < 0 and 1 for > 0. It does not negate the current value. Even if the code gets executed every frame, it will still always only set -1 or +1 accordingly.