FYI logo

Introduction to JavaScript and Basic Programming Logic

Before starting to co

By Hawwer DevPublished about a year ago β€’ 3 min read

JavaScript is one of the most popular programming languages in the world, essential for web development. With it, we can create dynamic, interactive, and responsive websites. Additionally, it allows us to manipulate HTML elements, create animations, validate forms, and even develop full applications for browsers and mobile devices. The Importance of Programming Logic Before starting to code, it is essential to understand the concept of programming logic. It is the ability to create a logical sequence of steps (algorithm) to solve a problem. In JavaScript, we work with variables, operators, and conditional structures, which form the foundation of any program. Mastering these concepts will greatly facilitate your learning and development as a programmer. Programming logic is also essential for solving real-world problems in a structured way. When we understand how to break a problem into smaller parts and solve it step by step, we become more efficient and creative programmers. First Steps with JavaScript Now, let's get hands-on! Here are the first concepts you need to know: Variable Declaration In JavaScript, we use let, const, or var to declare variables. πŸ‘‰ let: Used for variables that can change value. πŸ‘‰ const: Used for variables with fixed values. πŸ“š Example: let name = "Hawwer"; // Variable to store a name const age = 25; // Constant for age that does not change Using const is a good practice when we know that the variable value will not need to change, as it helps prevent accidental errors in the code. Arithmetic Operators The basic operators in JavaScript help us perform mathematical calculations. πŸ“š Example: let a = 5; let b = 10; let sum = a + b; // Addition let subtraction = a - b; // Subtraction let multiplication = a * b; // Multiplication let division = a / b; // Division console.log(sum); // Displays 15 console.log(subtraction); // Displays -5 Besides these operators, there are also the modulus operator (%), which returns the remainder of a division, and the increment (++) and decrement (--) operators. πŸ“š Example of modulus operator: let remainder = 10 % 3; // The result will be 1 console.log(remainder); Conditional Structures Conditional structures allow a program to make decisions based on certain conditions. πŸ“š Example: let age = 18; if (age >= 18) { console.log("You are an adult."); } else { console.log("You are a minor."); } Besides if and else, we can also use else if to test multiple conditions. πŸ“š Example with else if: let grade = 85; if (grade >= 90) { console.log("Excellent!"); } else if (grade >= 70) { console.log("Passed!"); } else { console.log("Failed!"); } Complete Example Let’s put everything we have learned so far into a simple program: let name = "Hawwer"; let age = 20; let result = ""; if (age >= 18) { result = name + " is an adult."; } else { result = name + " is a minor."; } console.log(result); πŸ“š Practical Exercises Now that you have learned the basic concepts, try the following exercises to reinforce your learning: 1. Create a program that stores your name and age in variables. Use a conditional structure to display whether you are an adult or a minor. 2. Create a program that takes two numbers and displays the results of their addition, subtraction, multiplication, and division. 3. Write a program that checks if a number is even or odd and displays the result in the console. 4. Create a program that asks the user to enter a grade from 0 to 100 and displays whether they passed, failed, or achieved excellence. Conclusion Today, you learned the first steps in JavaScript, including variables, operators, and conditional structures. These are the fundamental building blocks for developing more complex programs in the future. In the coming days, we will deepen our knowledge even further! Keep practicing and happy coding!

Vocal

About the Creator

Enjoyed the story? Support the Creator.

Subscribe for free to receive all their stories in your feed.

Subscribe For Free

Reader insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment
    Written by Hawwer Dev