What is Programming?
Programming is the art of giving instructions to a computer to solve problems and automate tasks. Just like following a recipe to bake a cake or giving directions to reach a destination, programming involves breaking down complex problems into simple, step-by-step instructions that a computer can understand and execute.
Every programming language shares fundamental concepts - think of them as the universal building blocks of programming. Whether you're writing in JavaScript, Python, Java, or any other language, you'll use these same core concepts:
These concepts work together like LEGO blocks - you combine them in different ways to build everything from simple calculators to complex web applications.
In this lesson, we'll explore each of these building blocks using JavaScript examples. Don't worry about understanding every detail of the JavaScript syntax yet - focus on grasping the underlying programming concepts. In the next section, we'll dive deeper into JavaScript specifically and learn its unique features and syntax.
A variable is a place to store values. When we write scripts (a set of instructions for a computer to follow), we need to temporarily store small pieces of data. We store that data in variables. “Variable” is a good name for this concept because it indicates the stored data can change (or vary) each time a script is run.
let amount = 9;
amount = amount * 2;
console.log(amount); // 18
// Convert `amount` to a string
amount = "$" + amount;
console.log(amount); // "$18"Variable Assignment
a = b * 2;a and b are called variables. Variables hold values to be used by the program:= and * are assignment and mathematic operators.a = b * 2; tells the computer to get the value stored in variable b, multiply that value by 2, then store the result back to the variable a.Math: + (Addition), - (Subtraction), * (Multiplication), / (Division), ** (Exponentiation), % (Modulus or Remainder), ++ (Increment), -- (Decrement)
Compound assignment: +=, -=, *=, /=. For example a += 2 is the same as a = a + 2
Equality: == (loose-equals), === (strict-equals), != (loose not-equals), !== (strict not-equals)
Comparison: < (less than), > (greater than), <= (less than or loose-equals), >= (greater than or loose-equals)
Logical: && (and), || (or)
let a = 1;
let b = a + 2;
b = b + 3;
console.log(b);Here are some example expressions:
"ap" + "ple"2 + 3Sometimes we want to perform an action based on some kind of condition. In English, we can say, “If this thing is true, then do that.” In JavaScript, conditionals are written very similarly and allow us to take a certain path in our program.
Let’s now look at the basic structure of a conditional:
if (expression) {
statements;
} else if (expression) {
statements;
} else {
statements;
}let today = new Date().getDay();
if (today === 6) {
console.log("Today is Saturday");
} else if (today === 0) {
console.log("Today is Sunday");
} else {
console.log("Looking forward to the Weekend");
}Repeating a set of actions until a particular condition fails is the job of programming loops.
for loop
Sometimes you are looping for the intended purpose of counting a particular set of numbers, like from 0 to 9 (10 numbers). You can do that by setting a loop iteration variable like i at value 0 and incrementing it by 1 for each iteration.
for (let i = 0; i <= 9; i = i + 1) {
console.log(i);
}
// 0 1 2 3 4 5 6 7 8 9while loop
while (numOfCustomers > 0) {
console.log("How may I help you?");
// help the customer...
numOfCustomers = numOfCustomers - 1;
}A function is generally a named section of code that can be “called” by name, and the code inside it will be run each time.
function toCelsius(fahrenheit) {
return (5 / 9) * (fahrenheit - 32);
}Functions can be used the same way as you use variables, in all types of formulas, assignments, and calculations. For example, instead of using a variable to store the return value of a function:
let x = toCelsius(77);
// String concatenation
let text = "The temperature is " + x + " Celsius";
// Template literal
// let text = `The temperature is ${x} Celsius`;You can use the function directly, as a variable value:
let text = "The temperature is " + toCelsius(77) + " Celsius";
// Template literal
// let text = `The temperature is ${toCelsius(77)} Celsius`;Now that you understand the fundamental building blocks of programming, you're ready to dive deeper into a specific programming language. In the next lesson, Intro to JavaScript, we'll explore:
These programming fundamentals you just learned are universal - you'll use variables, operators, conditionals, loops, and functions in JavaScript just like in any other programming language. The next lesson will show you how to apply these concepts using JavaScript's specific syntax and powerful features.