DEV Community

rajeshwari rajeshwari
rajeshwari rajeshwari

Posted on

🧠 Learn JavaScript: Looping, parseInt(), and Template Literals

Sure! Here's a simple blog post that introduces looping, parseInt, and template literals in JavaScript for beginners.


🧠 Learn JavaScript: Looping, parseInt(), and Template Literals

JavaScript is one of the most popular programming languages, especially for web development. If you're just getting started, it's important to understand some core concepts. In this blog post, we'll explore three essential JavaScript topics:

  • Looping (for loops)
  • parseInt() function
  • Template literals

🔁 1. Looping in JavaScript

Loops allow you to repeat a block of code multiple times. The most common type is the for loop.

Example:

for (let i = 0; i < 5; i++) {
  console.log("Count: " + i);
}
Enter fullscreen mode Exit fullscreen mode

What’s happening?

  • let i = 0 → Start at 0
  • i < 5 → Keep looping as long as i is less than 5
  • i++ → Increase i by 1 each time

🧪 Output:

Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Enter fullscreen mode Exit fullscreen mode

🔢 2. parseInt() - Convert String to Number

Sometimes you get numbers as strings (e.g., from input fields). You need to convert them to actual numbers to do math.

Example:

let age = "23";
let realAge = parseInt(age);
console.log(realAge + 2); // Outputs 25
Enter fullscreen mode Exit fullscreen mode

📝 parseInt() reads a string and converts it to an integer.


✨ 3. Template Literals

Template literals let you create strings more easily using backticks (` ``) and ${} to insert variables.

Example:

`javascript
let name = "Aisha";
let age = 23;
console.log(`My name is ${name} and I am ${age} years old.`);
`

🧪 Output:

`
My name is Aisha and I am 23 years old.
`

🧠 This is cleaner and more readable than using + to join strings.


🧵 Putting It All Together

Here's a small program using all three:

`javascript
let names = ["Ali", "Zara", "Ahmed"];
for (let i = 0; i < names.length; i++) {
let id = parseInt(i + 1);
console.log(`ID: ${id} - Name: ${names[i]}`);
}
`

🧪 Output:

`
ID: 1 - Name: Ali
ID: 2 - Name: Zara
ID: 3 - Name: Ahmed
`


✅ Summary

  • Use for loops to repeat actions.
  • Use parseInt() to convert strings to numbers.
  • Use template literals for cleaner string formatting.

Keep practicing! These tools are used in almost every real-world JavaScript program.


Would you like this blog post exported as a PDF or in a format for your website or social media?

Top comments (0)