In Ecclesiastes 7:3 (NASB)
Sorrow is better than laughter, For when a face is sad a heart may be happy.
What is the meaning of "For when a face is sad a heart may be happy"?
In קֹהֶ֣לֶת Kohelet | "Ecclesiastes" chapter 7:3, the Gatherer of wisdom states :
"Vexation is better than revelry; for though the face be sad, the heart may be glad." ( ט֥וֹב כַּ֖עַס מִשְּׂח֑וֹק כִּֽי־בְרֹ֥עַ פָּנִ֖ים יִ֥יטַב לֵֽב )
The לֵֽב Lev | "heart" refers to a person's rational faculties or a moral Neshamah (Proverbs 20:27).
If a person with a moral heart does not participate in traditional social customs, then that person's פָּנִ֖ים "face" may appear בְרֹ֥עַ "sad" for loss of friends.
Although the sad-faced person has lost friends, his/her "heart" will be "happy" because the rational Neshamah of that person knows he/she did not choose to break Torah through participating in traditional social customs.
[https://www.sefaria.org/Ecclesiastes.7.3?with=all&lang=bi]
Ecclesiastes 7 contains a set of philosophical paradoxes to get the readers to think deeper than the surface meanings, e.g., verse 2:
It is better to go to a house of mourning
Than to go to a house of feasting,
Because that is the end of every person,
And the living takes it to heart.
This is paralleled by verse 3:
Sorrow is better than laughter,
For when a face is sad a heart may be happy.
Sorrow is compared to mourning. Laughter is compared to feasting. When one hides his sadness, it causes internal turmoil. When one mourns and shows his sorrow, it heals the heart internally. It serves a cathartic release function. It calms the conscience. It is the smart and wise thing to do.
4 The mind of the wise is in the house of mourning,
While the mind of fools is in the house of pleasure.
Fools hide their hurts in pleasure. Smart people are not afraid to show it.