JavaScript/C2/Data-Types-and-Variables-in-JS/English
Title of the script:Data Types and Variables
Author: Jayesh Katta Ramalingaiah
Domain Reviewer: Ankita Maske
Novice Reviewer: Praveeen S.
Keywords: JavaScript, HTML, Data Types,Variables
|
|
Slide: Title | Hello and Welcome to the spoken tutorial on “Data Types and Variables in JS”. |
Slide: Learning Objectives | In this tutorial, we will learn about:
|
Slide: System Specifications | This tutorial is recorded using:
However you may use any other browser of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Data Types | Data Types are mainly classified into two types
|
Slide: Primitive Data Types | Primitive Data Type consists of
Let us take an example of each of these and understand better. |
Open Visual Studio Code editor. | I will use Visual Studio Code editor for this demonstration. |
[Editor]
Open Folder -> Practice-JS |
In the Welcome page, click on the Open folder link at the top left corner.
|
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named index.html
|
[Editor]
<!DOCTYPE html> <html lang="en"> <head> <title>Data Types & Variables</title> </head> <body> <script src="main.js"></script> </body> </html> |
In the index.html file, replace the code as shown.
|
Press: Ctrl + S | Save the file. |
Press: Alt + L and Alt + O
|
Now press the key combinations Alt + L and Alt + O.
|
Show firefox | The default browser will open automatically and a new tab opens. |
[Firefox] Press Ctrl + Shift + I
|
Now press Ctrl + Shift + I keys together.
|
[Firefox] Click on the Console tab | Now click on the console tab in the developer tools. |
Switch to Editor | Switch back to the editor. |
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named main.js |
[Editor] Type:
var b = "Spoken Tutorials!"; var c = true; var d;
console.log(typeof a); console.log(b); console.log(typeof b); console.log(c); console.log(typeof c); console.log(d); console.log(typeof d); |
In the main.js file, replace the code as shown. |
Press: Ctrl + S | Save the file.
|
[Editor] Highlight:
var |
var is a keyword which is used to declare variables.
|
[Editor] Highlight:
|
a is a name of a variable, which is assigned value number 10. |
Slide: Rules for variable name | Rules for variable name:
|
[Editor] Highlight:
|
Similarly b, is a variable name which is assigned a string value "Spoken Tutorials!"
|
[Editor] Highlight:
|
c is a variable name which is assigned boolean value true. |
[Editor] Highlight:
|
And d is a variable name which has no value assigned. |
[Editor] Highlight:
|
To display the value which the variable holds, just log the variable name. |
[Editor] Highlight:
|
To display the type of the value that the variable holds, use the typeof keyword. |
[Editor] Highlight:
console.log(typeof b); console.log(c); console.log(typeof c); console.log(d); console.log(typeof d); |
Similarly I have written code to display the other variables too. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
number |
We can see the output in the console where value of a is 10 and the type of a is number. |
[Browser] [Console Tab]:
Highlight:
string |
Similarly, the value of b is Spoken Tutorials! and the type of b is string. |
[Browser] [Console Tab]
Highlight:
boolean |
The value of c is true, and the type of c is boolean. |
[Browser] [Console Tab]
Highlight:
undefined |
For variable d, this is a special case in JS.
|
Slide: Non Primitive Data Types | Next, let’s look at Non-Primitive Data Types.
Non-Primitive Data Type consists of
Let us take an example of each of these and understand better. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
var g = [1,2, 'spoken', 'tutorials', 'js', true]; // var g = new Array();
console.log(typeof f); console.log(g); console.log(typeof g); console.log(f.name); console.log(g[2]); |
In the main.js file, replace the code as shown.
|
Only Narration | Let me explain this code. |
[Editor] Highlight:
|
To create an object in JS, we have to use curly braces.
|
[Editor] Highlight:
|
The code written after // (double slash) is a comment, which would not be interpreted.
|
[Editor] Highlight:
|
To create an array, we use square brackets.
|
[Editor] Highlight:
|
The code written in this comment is used to create an empty array. |
[Editor] Highlight:
console.log(typeof f); console.log(g); console.log(typeof g); |
Log statements are used to see the output. |
[Editor] Highlight:
|
To print the value that the property name holds we need to use a period. |
[Editor] Highlight:
|
Variable name f.name will display the value which the property holds. |
[Editor] Highlight:
|
To display the value of the nth index in an array, we need to pass the index.
|
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]
Highlight:
object |
Here, the prototype of f is printed as an object along with the value assigned to it.
|
[Browser] [Console Tab]
Highlight:
object |
Here, the prototype of g is printed as an array along with the value assigned to it.
|
[Browser] [Console Tab]:
Expand the ▶ arrow icon and Highlight:
0: 1 1: 2 2: "spoken" 3: "tutorials" 4: "js" 5: true length: 6 <prototype>: Array [] |
If we closely expand this, by clicking on the arrow icon in the console, we can understand the basic data structure.
|
[Browser] [Console Tab]:
Highlight:
|
Here, the value JS for name is printed using the period. |
[Browser] [Console Tab]
Highlight:
|
Here, the value of g at index 2 is printed. |
Switch to Editor | Switch back to the editor. |
Only Narration | As we understood, the data structure of both array and object is an object.
|
[Editor] Type:
… … …….. console.log(g[2]); console.log(f["name"]); console.log(g.length); |
In the main.js file, type these two console statements.
|
[Editor] Highlight:
|
f within square brackets, within quotes name"
|
[Editor] Highlight:
console.log(g.length); |
variable name g .(period) length will print the number of total values in the array. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight: JS |
Here, the same value JS is printed as output.
|
[Browser] [Console Tab]:
Highlight: 6 |
Here, we see the length printed as 6.
|
Only Narration | So, though the data types are different, the data structure is similar for both array and object.
|
Only narration | With this we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt about:
|
Slide: Assignment | As an assignment:
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Forum questions | Pls post your timed queries in this forum |
Slide: Acknowledgement | Spoken Tutorial Project is funded by Ministry of Education (MoE), Government of India |
Slide: Thanks | The script for this tutorial is contributed by Jayesh.
And this is Praveen from IIT Bombay signing off. Thank you for joining. |