The Gemara warns against excessive frivolity while also discussing the value of joy in Torah study. It helps set the boundary between wholesome humor and destructive levity.
וּמַאי ״דְּבָרָיו סוֹתְרִין זֶה אֶת זֶה״? כְּתִיב: ״טוֹב כַּעַס מִשְּׂחוֹק״, וּכְתִיב ״לִשְׂחוֹק אָמַרְתִּי מְהוֹלָל״! כְּתִיב ״וְשִׁבַּחְתִּי אֲנִי אֶת הַשִּׂמְחָה״, וּכְתִיב ״וּלְשִׂמְחָה מַה זֹּה עוֹשָׂה! לָא קַשְׁיָא ״טוֹב כַּעַס מִשְּׂחוֹק״: טוֹב כַּעַס שֶׁכּוֹעֵס הַקָּדוֹשׁ בָּרוּךְ הוּא עַל הַצַּדִּיקִים בָּעוֹלָם הַזֶּה, מִשְּׂחוֹק שֶׁמְּשַׂחֵק הַקָּדוֹשׁ בָּרוּךְ הוּא עַל הָרְשָׁעִים בָּעוֹלָם הַזֶּה. וְ״לִשְׁחוֹק אָמַרְתִּי מְהוֹלָל״ — זֶה שְׂחוֹק שֶׁמְּשַׂחֵק הַקָּדוֹשׁ בָּרוּךְ הוּא עִם הַצַּדִּיקִים בָּעוֹלָם הַבָּא. ״וְשִׁבַּחְתִּי אֲנִי אֶת הַשִּׂמְחָה״ — שִׂמְחָה שֶׁל מִצְוָה. ״וּלְשִׂמְחָה מַה זֹּה עוֹשָׂה״ — זוֹ שִׂמְחָה שֶׁאֵינָהּ שֶׁל מִצְוָה. לְלַמֶּדְךָ שֶׁאֵין שְׁכִינָה שׁוֹרָה לֹא מִתּוֹךְ עַצְבוּת וְלֹא מִתּוֹךְ עַצְלוּת וְלֹא מִתּוֹךְ שְׂחוֹק וְלֹא מִתּוֹךְ קַלּוּת רֹאשׁ וְלֹא מִתּוֹךְ שִׂיחָה וְלֹא מִתּוֹךְ דְּבָרִים בְּטֵלִים, אֶלָּא מִתּוֹךְ דְּבַר שִׂמְחָה שֶׁל מִצְוָה, שֶׁנֶּאֱמַר: ״וְעַתָּה קְחוּ לִי מְנַגֵּן וְהָיָה כְּנַגֵּן הַמְנַגֵּן וַתְּהִי עָלָיו יַד ה׳״. אָמַר רַב יְהוּדָה: וְכֵן לִדְבַר הֲלָכָה. אָמַר רָבָא: וְכֵן לַחֲלוֹם טוֹב.
And to the essence of the matter, the Gemara asks: What is the meaning of: Its statements that contradict each other? It is written: “Vexation is better than laughter” (Ecclesiastes 7:3), and it is written: “I said of laughter: It is praiseworthy” (Ecclesiastes 2:2), which is understood to mean that laughter is commendable. Likewise in one verse it is written: “So I commended mirth” (Ecclesiastes 8:15), and in another verse it is written: “And of mirth: What does it accomplish?” (Ecclesiastes 2:2). The Gemara answers: This is not difficult, as the contradiction can be resolved. Vexation is better than laughter means: The vexation of the Holy One, Blessed be He, toward the righteous in this world is preferable to the laughter which the Holy One, Blessed be He, laughs with the wicked in this world by showering them with goodness. I said of laughter: It is praiseworthy, that is the laughter which the Holy One, Blessed be He, laughs with the righteous in the World-to-Come. Similarly, “So I commended mirth,” that is the joy of a mitzva. “And of mirth: What does it accomplish?” that is joy that is not the joy of a mitzva. The praise of joy mentioned here is to teach you that the Divine Presence rests upon an individual neither from an atmosphere of sadness, nor from an atmosphere of laziness, nor from an atmosphere of laughter, nor from an atmosphere of frivolity, nor from an atmosphere of idle conversation, nor from an atmosphere of idle chatter, but rather from an atmosphere imbued with the joy of a mitzva. As it was stated with regard to Elisha that after he became angry at the king of Israel, his prophetic spirit left him until he requested: “But now bring me a minstrel; and it came to pass, when the minstrel played, that the hand of the Lord came upon him” (II Kings 3:15). Rav Yehuda said: And, so too, one should be joyful before stating a matter of halakha. Rava said: And, so too, one should be joyful before going to sleep in order to have a good dream.