What is the role of fear in Jewish battle?

א כִּֽי־תֵצֵ֨א לַמִּלְחָמָ֜ה עַל־אֹֽיְבֶ֗ךָ וְרָאִ֜יתָ ס֤וּס וָרֶ֨כֶב֙ עַ֚ם רַ֣ב מִמְּךָ֔ לֹ֥א תִירָ֖א מֵהֶ֑ם כִּֽי־יְהֹוָ֤ה אֱלֹהֶ֨יךָ֙ עִמָּ֔ךְ הַמַּֽעַלְךָ֖ מֵאֶ֥רֶץ מִצְרָֽיִם:

ב וְהָיָ֕ה כְּקָֽרָבְכֶ֖ם אֶל־הַמִּלְחָמָ֑ה וְנִגַּ֥שׁ הַכֹּהֵ֖ן וְדִבֶּ֥ר אֶל־הָעָֽם:

ג וְאָמַ֤ר אֲלֵהֶם֙ שְׁמַ֣ע יִשְׂרָאֵ֔ל אַתֶּ֨ם קְרֵבִ֥ים הַיּ֛וֹם לַמִּלְחָמָ֖ה עַל־אֹֽיְבֵיכֶ֑ם אַל־יֵרַ֣ךְ לְבַבְכֶ֗ם אַל־תִּֽירְא֧וּ וְאַל־תַּחְפְּז֛וּ וְאַל־תַּֽעַרְצ֖וּ מִפְּנֵיהֶֽם:

ד כִּ֚י יְהֹוָ֣ה אֱלֹֽהֵיכֶ֔ם הַֽהֹלֵ֖ךְ עִמָּכֶ֑ם לְהִלָּחֵ֥ם לָכֶ֛ם עִם־אֹֽיְבֵיכֶ֖ם לְהוֹשִׁ֥יעַ אֶתְכֶֽם:

1 When you go out to war against your enemies, and you see horse and chariot, a people more numerous than you, you shall not be afraid of them, for the Lord, your God is with you Who brought you up out of the land of Egypt.

2 And it will be, when you approach the battle, that the kohen shall come near, and speak to the people.

3 And he shall say to them, "Hear, O Israel, today you are approaching the battle against your enemies. Let your hearts not be faint; you shall not be afraid, and you shall not be alarmed, and you shall not be terrified because of them.

4 For the Lord, your God, is the One Who goes with you, to fight for you against your enemies, to save you.

How does the Torah define fear?

הירא ורך הלבב: רבי עקיבא אומר כמשמעו, שאינו יכול לעמוד בקשרי המלחמה ולראות חרב שלופה. רבי יוסי הגלילי אומר הירא מעבירות שבידו, ולכך תלתה לו תורה לחזור על בית וכרם ואשה לכסות על החוזרים בשביל עבירות שבידם, שלא יבינו שהם בעלי עבירה, והרואהו חוזר אומר שמא בנה בית או נטע כרם או ארש אשה:

Rabbi Akiva says: [This verse is to be understood] according to its apparent meaning, that he cannot stand in the closed ranks of battle and look upon a drawn sword. Rabbi Jose the Galilean says that [it means] one who is afraid of his sins [that they will cause him to fall in war, as he is unworthy], and therefore, the Torah gives him the excuse of attributing his return home because of a house, a vineyard, or a wife, to cover up for those who return because of their sins, so that people should not understand that they are sinners. [Consequently,] one who sees this person returning would say,“Perhaps he has built a house, or planted a vineyard, or betrothed a woman.” - [Sotah 44a]

תנא סח בין תפילה לתפילה עבירה היא בידו וחוזר עליה מערכי המלחמה

Concerning this, it is taught in a baraita: If one spoke between donning the phylacteries of the arm and the phylacteries of the head, he has a sin, and due to that sin he returns from the ranks of soldiers waging war. This is referring to the preparation for war, when the officers announce: “What man is there who is fearful and fainthearted? Let him go and return to his house” (Deuteronomy 20:8). The Sages explained that this is referring to one who is fearful due to his transgressions.

(ג) ד"א וירק את חניכיו, אין וירק אלא שהוריקן בדברים, כמו שכתוב בתורה מי האיש הירא ורך הלבב (דברים כ ח), כך עשה אברהם, אמר להם למלחמה אנו יוצאין, מי שיש בידו עבירות, וירא ממעשיו, אל יצא עמנו, כששמעו כך כל מי שהיה יודע בלבו שחטא היה מתירא ולא היה יוצא וחזרו להם, ולא נשאר עמו כי אם אליעזר בלבד, באותה שעה אמר הקב"ה, הניחו אותך הכל, ולא נשאר עמך כי אם אליעזר בלבד, חייך שאני נותן בו כח של שלש מאות ושמונה עשר, שנאמר וירק את חניכיו ילידי ביתו שמונה עשר ושלש מאות, מנין ספור אליעזר בגימטריא שלש מאות ושמונה עשר.

(3) Another interpretation (of Gen. 14:14): HE ARMED HIS TRAINED MEN. HE ARMED (yareq) means nothing but that he made them turn pale (horiq) with words, just as it is written in the Law (in Deut. 20:8): WHO IS THE ONE WHO IS AFRAID (yare) AND FAINT (rakh) OF HEART. Abraham did this: He said to them: We are going off to war. Let anyone who has transgressions on his hand and is afraid because of his actions not go off with us. When they heard that, anyone who knew in his heart that he had sinned was afraid and did not go off. So they went back, and no one was left with him (Abraham) but Eliezer alone.74Gen. R. 44:9; Numb. R. 18:21; PRE 27; M. Pss. 110:4. In that hour the Holy One said: All have forsaken you, and no one is left except Eliezer alone. By your life I am putting within him the power of three hundred and eighteen.75Cf. Targum Pseudo-Jonathan to Gen. 14:14. Thus it is stated (in Gen. 14:14) AND HE ARMED HIS TRAINED MEN, THE ONES BORN IN HIS HOUSE, THREE HUNDRED AND EIGHTEEN < OF THEM >. Where is it shown? The numerical value of Eliezer by gematria is three hundred and eighteen.

These sources think that problematic fear is one which would distract a man from battle. The reason someone would be this scared is if they have sinned. What is the danger of having men who are scared go into battle?

ח וְיָֽסְפ֣וּ הַשֹּֽׁטְרִים֘ לְדַבֵּ֣ר אֶל־הָעָם֒ וְאָֽמְר֗וּ מִֽי־הָאִ֤ישׁ הַיָּרֵא֙ וְרַ֣ךְ הַלֵּבָ֔ב יֵלֵ֖ךְ וְיָשֹׁ֣ב לְבֵית֑וֹ וְלֹ֥א יִמַּ֛ס אֶת־לְבַ֥ב אֶחָ֖יו כִּלְבָבֽוֹ:

(טו) (דברים כ, ח) "מִי הָאִישׁ הַיָּרֵא וְרַךְ הַלֵּבָב" כְּמַשְׁמָעוֹ. שֶׁאֵין בְּלִבּוֹ כֹּחַ לַעֲמֹד בְּקִשְׁרֵי הַמִּלְחָמָה. וּמֵאַחַר שֶׁיִּכָּנֵס בְּקִשְׁרֵי הַמִּלְחָמָה יִשָּׁעֵן עַל מִקְוֵה יִשְׂרָאֵל וּמוֹשִׁיעוֹ בְּעֵת צָרָה וְיֵדַע שֶׁעַל יִחוּד הַשֵּׁם הוּא עוֹשֶׂה מִלְחָמָה וְיָשִׂים נַפְשׁוֹ בְּכַפּוֹ וְלֹא יִירָא וְלֹא יִפְחָד וְלֹא יַחְשֹׁב לֹא בְּאִשְׁתּוֹ וְלֹא בְּבָנָיו אֶלָּא יִמְחֶה זִכְרוֹנָם מִלִּבּוֹ וְיִפָּנֶה מִכָּל דָּבָר לַמִּלְחָמָה. וְכָל הַמַּתְחִיל לַחְשֹׁב וּלְהַרְהֵר בַּמִּלְחָמָה וּמַבְהִיל עַצְמוֹ עוֹבֵר בְּלֹא תַּעֲשֶׂה. שֶׁנֶּאֱמַר (דברים כ, ג) "אַל יֵרַךְ לְבַבְכֶם אַל תִּירְאוּ וְאַל תַּחְפְּזוּ וְאַל תַּעַרְצוּ מִפְּנֵיהֶם". וְלֹא עוֹד אֶלָּא שֶׁכָּל דְּמֵי יִשְׂרָאֵל תְּלוּיִין בְּצַוָּארוֹ. וְאִם לֹא נִצֵּחַ וְלֹא עָשָׂה מִלְחָמָה בְּכָל לִבּוֹ וּבְכָל נַפְשׁוֹ. הֲרֵי זֶה כְּמִי שֶׁשָּׁפַךְ דְּמֵי הַכּל. שֶׁנֶּאֱמַר (דברים כ, ח) "וְלֹא יִמַּס אֶת לְבַב אֶחָיו כִּלְבָבוֹ". וַהֲרֵי מְפֹרָשׁ בַּקַּבָּלָה (ירמיה מח, י) "אָרוּר עשֶֹׁה מְלֶאכֶת ה' רְמִיָּה" וְאָרוּר מֹנֵעַ חַרְבּוֹ מִדָּם. וְכָל הַנִּלְחָם בְּכָל לִבּוֹ בְּלֹא פַּחַד וְתִהְיֶה כַּוָּנָתוֹ לְקַדֵּשׁ אֶת הַשֵּׁם בִּלְבַד. מֻבְטָח לוֹ שֶׁלֹּא יִמְצָא נֵזֶק וְלֹא תַּגִּיעֵהוּ רָעָה. וְיִבְנֶה לוֹ בַּיִת נָכוֹן בְּיִשְׂרָאֵל וְיִזְכֶּה לוֹ וּלְבָנָיו עַד עוֹלָם וְיִזְכֶּה לְחַיֵּי הָעוֹלָם הַבָּא. שֶׁנֶּאֱמַר (שמואל א כה, כח) "כִּי עָשֹׂה יַעֲשֶׂה ה' לַאדֹנִי בַּיִת נֶאֱמָן כִּי מִלְחֲמוֹת ה' אֲדֹנִי נִלְחָם וְרָעָה לֹא תִמָּצֵא בְךָ" וְגוֹ' (שמואל א כה, כט) "וְהָיְתָה נֶפֶשׁ אֲדֹנִי צְרוּרָה בִּצְרוֹר הַחַיִּים אֵת ה' אֱלֹהֶיךָ":

(15) "What man is there who is fearful and is faint-hearted" (Deut. 20:8)? Exactly so. This is one whose heart has no strength to withstand the strains of war68He is afraid he’ll be killed in battle due to his sins.. Now, once one becomes entangled with the perplexities of war, he should trust the Hope of Israel, and He will save him in times of trouble. He should know he is doing battle for the unity of the Name. He should place his life in His Hands and neither be afraid nor alarmed. He should not think about his wife or his children. Rather, he should wipe away thoughts of them from his heart, and clear himself of everything for the war. Anyone who thinks about or contemplates (matters) during the battle and frightens himself violates a Negative Commandment, as it says, “do not be faint of heart, nor be alarmed, do not flee, and be not frightened before them” (Deut. 20:3). And not only that, but all the blood of Israel hangs around his neck. If he did not win and did not fight the battle with all his heart and soul, he is like one who spills everyone’s blood, as it says, “lest he not melt his brothers’ hearts as has his” (Deut. 20:8). It is explicitly stated in the Tradition that, “Cursed be he who does the work of G-d sluggishly, and cursed be he who keeps back his sword from blood” (Jeremiah 48:10). However, anyone who does fight with all his heart without fear, and his intentions are only to Sanctify the Name, is promised that he will not be harmed and nothing bad will happen to him. He will build an upright home in Israel, and he and his children will earn merit forever, and merit The World To Come, as it says, “certainly, G-d shall make for my master a faithful house, for the wars of G-d does my master fight and no evil will be found with you all your days…And the soul of my master will be bound with the living, with the Lord your G-d…” (I Samuel 25:28-29).

(א) הָאֵל הַנִּכְבָּד וְהַנּוֹרָא הַזֶּה מִצְוָה לְאָהֳבוֹ וּלְיִרְאָה אוֹתוֹ שֶׁנֶּאֱמַר (דברים ו ה) "וְאָהַבְתָּ אֵת ה' אֱלֹהֶיךָ". וְנֶאֱמַר (דברים ו יג) "אֶת ה' אֱלֹהֶיךָ תִּירָא":

(ב) וְהֵיאַךְ הִיא הַדֶּרֶךְ לְאַהֲבָתוֹ וְיִרְאָתוֹ. בְּשָׁעָה שֶׁיִּתְבּוֹנֵן הָאָדָם בְּמַעֲשָׂיו וּבְרוּאָיו הַנִּפְלָאִים הַגְּדוֹלִים וְיִרְאֶה מֵהֶן חָכְמָתוֹ שֶׁאֵין לָהּ עֵרֶךְ וְלֹא קֵץ מִיָּד הוּא אוֹהֵב וּמְשַׁבֵּחַ וּמְפָאֵר וּמִתְאַוֶּה תַּאֲוָה גְּדוֹלָה לֵידַע הַשֵּׁם הַגָּדוֹל. כְּמוֹ שֶׁאָמַר דָּוִד (תהילים מב ג) "צָמְאָה נַפְשִׁי לֵאלֹהִים לְאֵל חָי". וּכְשֶׁמְּחַשֵּׁב בַּדְּבָרִים הָאֵלּוּ עַצְמָן מִיָּד הוּא נִרְתָּע לַאֲחוֹרָיו וִיפַחֵד וְיוֹדֵעַ שֶׁהוּא בְּרִיָּה קְטַנָּה שְׁפָלָה אֲפֵלָה עוֹמֶדֶת בְּדַעַת קַלָּה מְעוּטָה לִפְנֵי תְּמִים דֵּעוֹת. כְּמוֹ שֶׁאָמַר דָּוִד (תהילים ח ד) "כִּי אֶרְאֶה שָׁמֶיךָ מַעֲשֵׂי אֶצְבְּעֹתֶיךָ" (תהילים ח ה) "מָה אֱנוֹשׁ כִּי תִזְכְּרֶנּוּ". וּלְפִי הַדְּבָרִים הָאֵלּוּ אֲנִי מְבָאֵר כְּלָלִים גְּדוֹלִים מִמַּעֲשֵׂה רִבּוֹן הָעוֹלָמִים כְּדֵי שֶׁיִּהְיוּ פֶּתַח לַמֵּבִין לֶאֱהֹב אֶת הַשֵּׁם. כְּמוֹ שֶׁאָמְרוּ חֲכָמִים בְּעִנְיַן אַהֲבָה שֶׁמִּתּוֹךְ כָּךְ אַתָּה מַכִּיר אֶת מִי שֶׁאָמַר וְהָיָה הָעוֹלָם:

(1) It is mandatory to love and fear this Glorified and Awe-inspiring God, for it is said: "Thou shalt love the Lord thy God" (Deut. 6,5); and as it is said: "The Lord thy God thou shalt fear." (Ibid. 6, 13).

(2) But how may one discover the way to love and fear Him? When man will reflect concerning His works, and His great and wonderful creatures,1But he must make thorough study. C. and will behold through them His wonderful, matchless and infinite wisdom, he will spontaneously be filled with love, praise and exaltation and become possessed of a great longing to know the Great Name, even as David said: "My soul thirsts for God, for the living God," (Ps. 42,2); and when he will think of all these matters,2Sifre, Deut. 6.5; Shabbat, 30–31. G. he will be taken aback in a moment and stricken with awe, and realize that he is an infinitesimal creature, humble and dark, standing with an insignificant and slight knowledge in the presence of the All Wise, as David said: "For when I see Thy heavens, the wonderful works of Thy fingers, of what use is man that Thou mayest remember him?" (Ibid. 8,4). And, in harmony with these matters, I elucidate great, general principles of the works of the Lord of the universe, so that they might serve as an opening for one who understands by which to love the Name, as some sages said on the subject of love: "Out of it thou wilt recognize the One who spoke, and the universe was called into existence."

If we are commanded to fear god how can we go to war without fear?
Our fear should be of hasher’s power, strength and control. We must fear his capabilities to control the outcome of the war soldiers are going into.
The other fear, the negative fear, would be our own fear for ourselves because of the wrath of hashem we feel we may feel due to our sins.
The first type of fear is the positive fear, in which all soldiers need in order to combat the fear of being killed in battle for his sins. The fear of Hashem destroying our enemies and protecting us should combat the fear of dying in battle. The fear we are commanded to have is that which should strengthen our belief in hashem; that he is capable of everything and anything. Hopefully the positive fear can cancel out fear of going into battle. And if not, then we leave battle due to our fear of god, of god punishing us.

It seems that we are forbidding a nervous man from entering battle so that he does not influence other soldiers negatively, so they can all concentrate during war. From here we learn that fear is healthy, but only in moderation. Too much can distract us from the larger task at hand.

דברי הימים א כ״ח:ב׳-ג׳
(ב) וַיָּ֨קָם דָּוִ֤יד הַמֶּ֙לֶךְ֙ עַל־רַגְלָ֔יו וַיֹּ֕אמֶר שְׁמָע֖וּנִי אַחַ֣י וְעַמִּ֑י אֲנִ֣י עִם־לְבָבִ֡י לִבְנוֹת֩ בֵּ֨ית מְנוּחָ֜ה לַאֲר֣וֹן בְּרִית־יְהוָ֗ה וְלַהֲדֹם֙ רַגְלֵ֣י אֱלֹהֵ֔ינוּ וַהֲכִינ֖וֹתִי לִבְנֽוֹת׃ (ג)וְהָאֱלֹהִים֙ אָ֣מַר לִ֔י לֹא־תִבְנֶ֥ה בַ֖יִת לִשְׁמִ֑י כִּ֣י אִ֧ישׁ מִלְחָמ֛וֹת אַ֖תָּה וְדָמִ֥ים שָׁפָֽכְתָּ׃

I Chronicles 28:2-3
(2) Then David the king stood up upon his feet, and said: ‘Hear me, my brethren, and my people; as for me, it was in my heart to build a house of rest for the ark of the covenant of the LORD, and for the footstool of our God; and I had made ready for the building. (3) But God said unto me: Thou shalt not build a house for My name, because thou art a man of war, and hast shed blood.

This offers helpful insight into the story of David, and the reason he was not able to build the Beit Hamikdash. With the knowledge that a small amount of fear is helpful, we can infer that David felt too comfortable on the battlefield. We know that the ideal state for our world is peace-

When Daniel entered the lions’ den it was the lions that were afraid of his G-dly presence. Similarly, when the children of Yaakov exited from Schem the verse testifies, “They set out and, and there fell a G-dly terror on the cities which were around them, so they did not pursue Yaakov’s sons.” (Breishis 35:5) Why was it necessary to mention that it was the “G-dly terror that kept the surrounding enemies at bay? Shimon and Levi had just flexed their militant muscles! That probably contributed more to their exposure to danger. It certainly wasn’t their saving grace. However, when the “G-dly Image” is intact and untainted the oppositional forces are more scared of us than we of them. We are told to not be afraid of “them”-the enemy, but rather to be fearful of countermanding HASHEM. When we fear G-d, they fear us, because either way fear is contagious.

However, in times where we must go to war, a moderation of fear is necessary.