اسرع طريقة لتحويل أي قيمة لرقم في JavaScript | One-Line JS Trick

اسرع طريقة لتحويل أي قيمة لرقم في JavaScript | One-Line JS Trick

Today we have a small trick in JavaScript. Very useful and will help you a lot. If we want to convert any value to a number, what do we do? We put it inside Number() or we use parseInt. So we use Number(), open the parentheses, and put what we want… or we use parseInt. But without all of that… if you want something quick, and lets you convert any value you give it to a number… We’ll do something very simple. We have a string here… we put a + before it. So you’re telling me it becomes a number? Yes… that’s it. That’s the whole thing. It should give me 42 as a number. Okay, what about true? true is also a value… it’s 1. When I put + before it, it converts. So true should give me what? It gives me 1. And false gives me what? It gives me 0. So I converted them? Yes, I converted them. How do we make sure? We used the + instead of Number(). The + forces any value to turn into a number. So 42 the string became a number. true became a number. false became a number. This is very useful when you want to convert something quickly. You use it in comparisons, you use it in any calculation… it simplifies everything. Let’s print and see if what we’re saying is true or not. If we take the 42 with the + and put it here… and take true… and take false… and put them… We save… we print in the console… it tells me: 42, 1, 0. Try it yourself, and if you have any questions, leave them in the comments. في الفيديو ده بشرح لك تركة بسيطة جدًا لكنها قوية في JavaScript. التركة دي بتخليكي تقدري تحوّلي أي قيمة لـ Number بسطر واحد من غير ما تستخدمي Number() أو parseInt. مجرد ما تحطي علامة + قبل أي قيمة… فورًا بتتحول لرقم. والموضوع ده بيوفّر وقت جدًا وبيسهّل المقارنات والعمليات الحسابية في الكود #javascript #coding #webdevelopment #learnjavascript #frontend