Morgcode

Introduction to JavaScript for New Programmers

JavaScript is one of the most popular programming languages in the world. If you have ever browsed the web, it is very likely that you have interacted with something made in JavaScript. It is used to create dynamic web pages, adding interactivity and making pages more appealing and functional. In this article, we will introduce the fundamental concepts of the language, focusing on variables and mathematical operations so that you can take your first steps in learning JavaScript.

What is JavaScript?

JavaScript is a lightweight, interpreted, and text-based programming language used on both the client side (browsers) and the server side (with Node.js). It allows you to create interactive and dynamic websites, meaning sites that respond to user actions without needing to reload the entire page. Along with HTML and CSS, JavaScript is one of the three main technologies of the web.

Where is JavaScript Used?

JavaScript is widely used for various purposes:

Creating and Using Variables

A variable in JavaScript is like a container that holds data. You can store different types of values in variables, such as numbers, text (strings), and then use them in your program. Variables can be declared in three main ways: let, const, and var.

Example of Variable Declaration:

let name = "Gustavo";  // A variable of type string (text)
const age = 25;      // A constant of type number
var city = "São Paulo"; // Using var (prior to ES6)

let is used when the variable's value can change.

const is used for values that should not change.

var was the main way to declare variables before ES6, but it is now less used due to scope limitations.

Simple Mathematical Operations

JavaScript allows performing basic mathematical operations very easily. You can do addition, subtraction, multiplication, and division using simple operators.

Example of Operations:

let a = 10;
let b = 5;

let sum = a + b;            // 15
let subtraction = a - b;       // 5
let multiplication = a * b;   // 50
let division = a / b;         // 2

Practical Usage Example

Here is a simple example of how you can use variables and mathematical operations together to create interactivity on a web page:

let productPrice = 50;
let discount = 10;

let finalPrice = productPrice - discount;

document.getElementById("price").innerHTML = "The final price is: $" + finalPrice;
    

Conclusion

JavaScript is a powerful tool for creating dynamic and interactive websites, whether manipulating content, performing calculations, or generating new functionalities. The ability to create and use variables, perform basic mathematical operations, and interact with the DOM (Document Object Model) are the pillars for building more complex applications.

Keep practicing these fundamental concepts and explore other topics like arrays, loops, and functions to further develop your JavaScript skills.