Transcript for:
JavaScript Basics: Variables and Functions

Hi everyone, I am Rakesh from 2nd year CSC. I am here to complete my full stack capstone assignment. Let's start with creating a file.

Let me name it variable.js Variables can be declared using three keywords in JavaScript var, let and constant. Let's see how they work. Let me write the code here.

To get the output, the variables declared using var can be redeclared and updated. here is redeclared though it is redeclared and reassigned we can get the output see here variables declared using let can be updated but cannot be redeclared let me show you Here A is declared two times, it shows an error. But it can be updated. Here A is updated with value 200. Variables declared using constant cannot change its value. If I change the value of A, we get an error in the output.

Yes, we can store many elements of similar data type. Let's see some code on array. If I want to print it, it gets printed in the form of array. We can also manipulate arrays by using indexes. Here I changed the element with index 0 to 100. Again I print it.

We can also print individual elements in the array by giving its index. Let me run it. Here the output comes. Functions are used when we want to use a set of code multiple times.

Here I am writing a code to multiply numbers by 3. It is the block of function to multiply the number. Set a number into 3. I am declaring a number to give it 10. As input in console printing statement I will call the function multiply by 3. I'm giving another variable as input for the function. I called the same function two times here and I will run it now.

Here is the output which is multiplied by 3. objects let me create a file objects.js we can access properties to an object using dot and bracket notation let us see them here is a dog object in which I gave some properties to use like legs In console I am printing the data in legs property using dot notation and using bracket notation I will print tails property it is bracket notation. You can also update the object properties. Let's change the data and mode property of the dog object. is better output is better let's update the mode property updating object properties dot mode equals to whatever the data we want is to i will print the data in mode property Now I will run it.

That is the output. Loops. Let us create a new file loops.js We have three loops while loop, do while loop, for loop.

Now I will write code to implement each loop. Let's start with while loop. I will write a code to print the first five natural numbers using the three loops.

loops here is the while loop I'm printing using the variable I this is the increment statement if I run it here are the natural numbers 1 to 5 this is using while loop I am commenting this out in the same file I will write the other loops I will write for loop for now for where i equals to 1 1 to 5 i want 1 to 5 so i am starting the variable i from 1 in console i am writing i if i compile here are the numbers again 1 to 5 let me show you clearly this is using far loop I commented that out it won't execute now using far loop this is the output 1 2 3 4 5 and commenting the far loop again I will write the dual loop this is do a loop do the statements what we want to write and at last after the after coming out from the loop we will write the while condition here it is this is using while loop no it's not while it's do while we will write do while loop ok ok that's it for now thank you