JavaScript is an extremely cool programming language and has been around for a while now. It can be used to write servers, mobile apps, desktop apps, web apps, websites, scripts, and more. JavaScript is also among the easiest programming languages. It is widely used. And today, we'll teach you the basics of JavaScript!
When you attempt to modify a constant variable message here for example, you see a error. The code above it works completely fine. However, you are able to modify msg, which is a let variable.
Creator and History
It was invented in 1995 by Brendan Eich. It was originally called "Mocha". Eich created JavaScript in just two weeks while working as a developer at Netscape. The language was developed to extend the early web beyond the limits of HTML.The EXTREME basics!
First: You need to know where you'll write it before actually writing it. You can simply do it in a new tab of your browser. If you're on a mobile, try downloading a code editor such as Acode.
Open a new tab and hit
Ctrl+Shift+I
. This should open the inspect feature or devtools. If it doesn't simply right click on your mouse and choose Inspect.console.log("hello world");
Your console should output "hello world". Cool, right? That's how you print text!Explanation
console tells JavaScript that the line of code is related to the console. log tells it to log something in the console. Then, we add parentheses () and double quotes to add our text. We only add double quotes for strings. For data such as numbers, we can directly type them in. For example:
console.log(7);
This will print 7 into the console, without us having to add parenthesis.
Some math
Time to do some math. Let's see what 1+1 is, using JavaScript.
console.log(1+1);
Oh yeah, now you know that 1+1 is 2. Let's try multiplying.console.log(2*2);
2 Multiplied by 2 is indeed, 4, as the output probably says. You can already see how cool programming and JavaScript is, right?If you're wondering when we'll build actually apps, websites, and programs, have patience. We'll get to it soon too!
Dividing
You can divide using a forward slash (/).
console.log(4/2);
4 Divided by 2 is equal to 2, as the output says. Variables
We're now getting to variables. They allow you to store a value for later use. It can be a number, string or even a boolean.
But what's a boolean?
A boolean is either true or false. It is used when you need to compare two conditions. We'll get into it later.
Types of variables
There's 3 types of variables, but you only need to know 2 for now. const and let are the two main types.
Writing a variable
It's time to write your first variable!
const msg = "hello world!";
console.log(msg);
Now since we're writing multiple lines, make sure to click Shift+Enter in the console to start a new line. Clicking just enter is gonna run the code!In the above code, we declare a constant variable using const. msg is the variable name. The variable name can be anything without a space - anything! you can even name it anything, literally! And then we give it a string value of "hello world". We can also write a number!
However, this way, the variable will just sit there alone. So we add console.log to log its value! Notice how we didn't put the double quotes? It's because we're telling the program to access the value of the variable msg. If we added double quotes, it would output "msg" instead of "hello world!"
Difference between a const and a let variable
We created a const variable in the last example. A let variable is created the same way, but we write let instead of const.
let msg = "hello world!";
console.log(msg);
See? That's how simple it is to create a let variable. But what's the difference?A const variable is constant, meaning its value can't change. However, a let variable's value can change. Try copy pasting the example below and running it:
This will be the output:
let msg = "hello world!";
const message = "yo world!";
console.log(msg);
console.log(message);
msg = "bye world!";
console.log(msg);
message = "yo people!";
console.log(message);
This will be the output:
When you attempt to modify a constant variable message here for example, you see a error. The code above it works completely fine. However, you are able to modify msg, which is a let variable.
Maths with variables
It's time to do some maths using two variables. We will create two const variables x and y, and mutliply them inside a console.log.
const x = 10;
const y = 10;
console.log(x*y);
The output should be 100, since x*y(10*10) is 100.Text and Variable
Now, how about we try logging to the console some text and variable? Let's do some maths first.
Sam is 10 years older than his dad, who is 12 years old. Calculate Sam's age.
const dadage = 12;
const samage = dadage+10;
console.log("Sam's age is " + samage + " years.");
The output should be "Sam's age is 22 years."First, we put the text in double quotes, then add a + sign and the variable name, and then add another plus sign and more text in double quotes. The plus sign tells JavaScript to join text if it's a string and do addition if its two numbers.