This is what we call a “General Reference” question around here. It’s simply because why can be used as an interjection. Any good dictionary should have told you that. Here’s what the OED says about this sense of why:
7. Used interjectionally, before a sentence or clause.
- a. As an expression of surprise (sometimes only momentary or slight; sometimes involving protest), either in reply to a remark or question, or on perceiving something unexpected.
- ?1520 J. Rastell Nature .iiii. Elem. sig. Bvij, Than I perceyue ye wyll make gode chere Hu. why what shulde I els do?
- 1581 Confer. with Campion (1583) C iij, Why, is not Saint Iames Epistle called the Catholike Epistle of Saint Iames. How do you then denie it to be Canonicall?
- 1600 Shakespeare Much Ado about Nothing ɪᴠ. ii. 39 Why this is flat periurie, to call a Princes brother villaine.
- 1600 Shakespeare Much Ado about Nothing ᴠ. iv. 74 Bene. Do not you loue me? Beat. Why no, no more then reason.
- 1611 Bible (A.V.) Matt. xxvii. 23 They all sayde vnto him, Let him be crucified. And the Gouernour said, Why, what euil hath he done?
- 1712 R. Steele Spectator No. 533. ⁋1 What do I think? why, I think she cannot be above six foot two inches high.
- 1779 J. Warner in J. H. Jesse G. Selwyn & his Contemp. (1844) IV. 274 What was I to do in this more than Egyptian darkness? Why, go to bed. Very true.
- 1837 Dickens Pickwick Papers xxxviii. 417 ‘Goodness gracious!’ said Mary,‥ ‘Why it's that very house.’
- 1847 T. De Quincey Secret Societies in Tait's Edinb. Mag. Oct. 667/1 Were there no such people as the Essenes? Why, no; not as Josephus described them.
- 1863 C. Kingsley Water-babies iii. 103 And, as he spoke, he turned quite pale, and then quite white. ‘Why, you're ill!’ said Tom.
- 1893 M. Pemberton Iron Pirate i, Mary looked up suddenly‥and said,‥‘Why, I believe I've been asleep!’
- b. Emphasizing or calling more or less abrupt attention to the statement following (as in the apodosis of a sentence), in opposition to a possible or vaguely apprehended doubt or objection.
- 1545 T. Raynald tr. E. Roesslin Byrth of Mankynde 90 When she feleth greate ache in the inner parte of the eyes‥, yᵉ reste of the body taken as it were with a werynesse without any outwarde apparent cause: why these thynges portende‥aborcement to be at hande.
- 1590 T. Lodge Rosalynde (1592) N 2 b, And to conceale it, why it doubled her griefe.
- 1594 1st Pt. Contention ɪɪ. i, Why let me see, I thinke thou canst not see yet.
- a1596 Sir T. More (Malone Soc.) ɪ. i. 122 Take an honest woman from her husband! why, it is intollerable.
- 1604 Shakespeare Hamlet ɪ. ii. 121 Ham. I shall in all my best obay you Madam. King. Why tis a louing and a faire reply.
- 1604 Shakespeare Hamlet ɪɪɪ. ii. 259 Why let the strooken Deere goe weepe, The Hart vngauled play.
- a1616 Shakespeare Two Gentlemen of Verona ɪɪɪ (1623) ɪ. i. 33 If hap'ly won, perhaps a haplesse gaine, If lost, why then a grieuous labour won.
- 1647 A. Cowley Request in Mistress iii, If her chill heart I cannot move, Why, I'le enjoy the very Love.
- 1719 Swift Quiet Life in Wks. (1735) II. 351 Why Dick, thy Wife has dev'lish whims.
- 1769 O. Goldsmith Rom. Hist. I. 439 If you will have Caesar for your master, why have him.
- 1841 Dickens Old Curiosity Shop ɪ. i. 42 ‘A long way, wasn't it, Kit?’‥ ‘Why then, it was a goodish stretch, master,’ returned Kit.
- 1863 C. Kingsley Water-babies iii. 103 If she chooses to come, why she * may; and if not, why I go without her.
- 1869 G. J. Whyte-Melville Songs & Verses 93 So he made for the gate,‥And the chain being round it, why—over he flew!
- 1882 W. Besant All Sorts of Men II. xxiii. 139 ‘Not a doubt,’ added the Professor. ‘Why, it stands to reason.’
- †c. As an emphasized call or summons, expressing some degree of impatience. Obs.
- 1599 Shakespeare Romeo & Juliet ɪᴠ. iv. 29 Mistris, what mistris, Iuliet‥Why Lambe, why Lady, fie you sluggabed, Why Loue I say‥why Bride.
- 1600 Shakespeare Merchant of Venice ɪɪ. v. 6 What Iessica.‥ Why Iessica I say.
- 1600 Shakespeare Henry IV, Pt. 2 ᴠ. i. 6 What Dauy I say.‥ Why Dauy.
- †d. why, so! an expression of content, acquiescence, or relief. Obs. or arch.
- 1597 Shakespeare Richard II ɪɪ. ii. 87 Seruingman. My Lord, your son was gone before I came. Yorke. He was; why so go all which way it will.
- a1616 Shakespeare Taming of Shrew (1623) iv. iii. 194 Pet.‥It shall be what a clock I say it is. Hor. Why so this gallant will command the sunne.
- a1616 Shakespeare Macbeth (1623) ɪɪɪ. iv. 106 Hence, horrible shadow.‥[Ghost vanishes.] Why, so, being gone, I am a man againe.
- 1826 Scott Woodstock I. iii. 65 If you will have the things rendered even now, why so—and if not, hold me blameless.