In this section, we will dive into actually javascript programming. I am assuming that you already know the definition of javascript and its importance in contemporary modern web development. Also, if you are a new javascript, I would suggest you use Playcode, which will help you tag along with me while you are still in the browser; if you are wondering how to configure it, here is the hello world post via Playcode.
Values
In this section, I will be using persons as examples, like a person's name, age, or job. But anyway, let's talk about values now. A value is basically a piece of data, so it's the most fundamental unit of information that we have in programming.
'Naruto'
"Naruto"
is same as 'Naruto'
, but using 'Naruto'
will give you an advantage and can be seen as we move forward in this course. So, for now, use this convention for writing any string.
console.log('Naruto');
script.js
file. We will use console.log
so frequently that you will get the hang of it in no time. Also, did you notice that I used a semicolon at the end of the line console.log
; in javascript, it is not mandatory to use a semicolon at the end of each line, but it is considered a good coding practice. The semicolon in javascript programming is analogous to period
(".") in framing an English sentence.
console.log('Naruto');
console.log('Itachi');
What about integers?
// String Values
console.log('Naurto');
console.log('Itachi');
// Number or Integer values
console.log(6774);
console.log(37);
// Anything within single quotes is a string
// Here even this number is considered as string
console.log('32');
How can a number be a string?
What does that even mean?
Let's have a closer look at these by introducing a simple Mathematical Operation, the addition.
// In case of integeres, "+" is considered as mathematical operation
console.log(4 + 5); // 9
// In case of strings, "+" is considered as string concatination
console.log('4' + '5'); // 45
// Adding Strings or Concatinating Strings
console.log('Akhil' + 'Naidu'); // AkhilNaidu
// We can even concatinate blank space
console.log('Akhil' + ' ' + 'Naidu'); // Akhil Naidu
Now, one extremely important thing that we can do with Values is to store them into Variables. And with this way, we can re-use them again and again.
Variables
In order to create a variable in javascript, we can use an inbuilt keyword let
, which allows us to name the variable. Go through the below-mentioned code sample line by line for more information.
I like to imagine a variable as a box. In the real world, a box can hold some object; for example a book. And then we can write a label on the box to describe what's in it. And then we can find the object when we need it, by using that label. In javascript variables also work in the exact same way.
let hero = 'Naruto';
Here we have a box called a hero and into the box, we put the value of Naruto. And now if we want to use this value, all we have to do is to use this label or in other words this variable name; hero.
// Assining the value Naruto to the variable hero
let hero = 'Naruto';
console.log(hero); // Naruto
let hero = 'Naruto';
console.log(hero); // Naruto
// We can also re-assign
hero = 'Itachi';
console.log(hero); // Itachi
// We can even change reassign a different type of data
hero = 2; // string to integer; changing the type of data
console.log(hero); // 2
Now, let's combine our knowledge of string concatenation and variables and see what we can do.
// Creating a varibale of name "firstName"
let firstName;
// Assigning the value, "Akhil" to the variable
firstName = 'Akhil'; // Single quote, so this is string
// To check you can use console.log(variableName);
console.log(firstName); // Akhil
// Creating varible and assigning a string value at the same time
// Also called as declaring a variable
let secondName = 'Naidu';
console.log(firstName + ' ' + secondName); // Akhil Naidu
Most of the time, we will declare a variable rather than just creating it; here is a classic example that uses both declaring and creating simultaneously.
let number1 = 4; // No single quotes, so number1 is an integer
let num2 = 5; // We can choose our own variable naming convention
let total; // Just creating the variable
total = number1 + num2; // based upon number1 and num2, total varies
console.log(total); // 9
One of the biggest advantages of using variables is its ability to be used at multiple places. How? For example, rather than manually changing a particular repeated value manually at multiple places, I can create a variable and assign it a value and place this variable, wherever I need this value. This way, whenever I reassign the variable, all the other places will be updated automatically.