Bears are a source of anxiety for many hikers visiting Alaska. Although instinctively afraid of humans, in certain circumstances bears will attack.
Over the years I encountered a few bears along the trail and fortunately they either heard or saw me with enough distance and moved away with no bad outcomes.
Prevention is the best strategy, your voice is a natural deterrent and making plenty of noise on the trail will let bears know you are nearby. They will likely disappear without you ever noticing.
I routinely encourage all guests at our hostel who are hiking in Alaska to take bear spray with them. They are extremely effective as a deterrent and it is reassuring to read this recent article which found bear spray was more effective than guns. The study looked at actual bear encounters with hikers and hunters. I have attached the link below.