- Variable
- Conditional Statement
- Array , list or collection
- Loop
- Function
1. Variable :
Variables are used to store data. Every programming language has variables. In JavaScript, we declare a variable with the var keyword. Let’s look at the example below :
var a = 6; var b = 3;
Here a is a variable which has a value 5. We can change the value at any time. Like this :
const y = 54;
i. Difference between var , let and const.
In JavaScript, we can declare a variable with 3 keywords. They are var, let, and const. Now the question is why these 3 keywords? We can declare a variable with 1 keyword. Let’s see what is the difference between these three keywords.
1. var declaration is global scoped or function scoped. On the other hand let and const are block-scoped
2. var variables can be re-declared or can be updated, let variables can be updated but can’t be re-declared and the last one const neither be updated nor re-declared.
Now we know how to declare a variable. How will we show that variable? Let’s see how to show output in JavaScript
ii. JavaScript Output
In JS we can show output in 4 different ways. They are :
1. Through innerHTML;
var element = document.getElementbyId('demo');
element.innerHTML = “something”;
2. Write method ;
document.write(“something”);
3. Though alert;
window.alert(‘something’);
4. Through console;
console.log(‘something’)
Now we know how to declare a variable and show them. Let’s jump into another basic concept of javascript.
2. Conditional Statement :
Conditional statement is very important in a programming language. It means they do something based on a condition. For example, you want to buy a mobile phone. Now if you have more than $100 then you will buy a smartphone otherwise you will buy a feature ( button ) phone. Let’s see it in a code example:
var money = 140;
if(money > 100){buySmartPhone() //this part will execute}
else{buyButtonPhone()}
We can write multiple layers of condition. For example, if you have less than $20 you will buy a piece of bread or if you have more than $20 but less than $30 you will buy a little 2 layer burger or if you have more than $30 you will buy a big 3 layer burger. Let’s see it in code :
var money = $25;
if(money < 20){buyBread()}
else if(money > 20 && money < 30){buyLittleBurger()}
else if(money > 30){buyBigBurger()}
Writing these multiple layers of conditions is tiring, isn’t it? For this, we have switch statement. We will discuss this in another article. By the way && is a conditional operator it means both conditions have to be true to execute the code in curly brackets.
3. Array :
The array is a very important concept in every programming language. An array can store multiple values where a variable can store a single data. In array, you can access the values separately. Let’s see an example :
var laptop = [“HP”, “Dell”, “Asus”]
document.write(laptop[0]); //print HP
If you want to store these 3 strings in normal variables you need 3 variables. But only one array can store these 3 or even more. Basically, it is used for storing a collection of data.
4. Loop :
Loop is also an important concept for programming language. Loop means do the same work multiple times. In javascipt, mainly we have 3 types of loop
- for loop
- while loop
- do while loop
Let’s see the syntax for the loops
//For loop
for(var i = 0; i ≤ 10; i++){
document.write('I am a programmer');
}
//while loop
var i = 1;
while(i <= 10){
document.write('I am a programmer');
i++;
}
//do while loop
do{
document.write('I am a programmer');
i++;
}
while(i <= 10)
5. Function :
You can imagine a function like a machine. For example the coffee maker. You put the necessary thing in the machine and you get the output as coffee. In the function you put the necessary things (parameter) and you get the output. Let’s have an example :
function square(num){
return num*n;
}
square(2); // 4
This is how a function works and what it’s syntax look like. A function is also used to remove duplicate code. You put your necessary code in a function, you can use that throughout the program.
That’s all about today. I will provide more articles on javascript. If I have done any mistakes in this article please let me know so that we can learn the correct thing together.
Happy Coding ❤;
2 Comments
Casino Night - MapyRO
ReplyDeleteFind your perfect game at Harrah's Resort and Casino in Robinsonville, 정읍 출장샵 MS. See 24/7 customer service 포천 출장마사지 and online Play Blackjack, Slots, Roulette and Video Poker. Rating: 대구광역 출장샵 4.3 원주 출장안마 · 대전광역 출장마사지 25 votes
Please enable JavaScript or change to a supported browser to proceed utilizing twitter.com. You can see a listing of supported browsers in our Help Center. Playlive.com needs to evaluation the security of your connection earlier than proceeding. 코인카지노 A 310-room lodge and convention center opened on June 6, 2018, on a site adjoining to the on line casino constructing. Casino, is a on line casino lodge in Hanover, Maryland, adjoining to Arundel Mills Mall.
ReplyDelete