hello guys welcome to the next blog on javaScript tutorial for beginners in this blog we will learn about data types in JavaScript so there are basically five data types in JavaScript which is strings numbers boolean arrays and objects arrays an object we will learn in further blogs and in this blog we will discuss about string numbers and boolean data types so to declare variable in JavaScript you need to declare it with a keyword called var and it's like a data type in JavaScript so unlike in other languages like Java or C++ you literally give the data type like int or bool or string here but in JavaScript you give just this keyword called var and then your variable name so for example my number and then you can initialize this variable by some value for example 10 and by initialization JavaScript deduce the data type of a variable so here it's initialized with a number so this data type of this variable will be a number right now let's print this value so there is a function in JavaScript which is called alert and it takes one argument it can be a string or a number or anything right so to print a variable you just need to copy it and pass it as an argument of this alert function and save your code and refresh your webpage and it gives you this number in the form of alert ok now this number can be of different type for example if you want to initialize this variable by a decimal number or floating point you can use it something like 10 point 2 1 3 or some decimal points right or it can be a exponent value so you can initialize it something like 10 e5 which means 10 into 10 to the power 5 so let's save it and refresh our webpage it will give you this value right or you can do something like 10 e minus 5 which means 10 multiplied by 10 to the power minus 5 and save it and refresh our page and it gives you this decimal value right now to declare a boolean value in JavaScript you do the same so var the variable name my bowl and then you declare it with true or false so it can be true or it can be a false value ok so a boolean is a data type which can contain either true or false so this time we are going to initialize it by true for example and just pass it in here in the alert argument and save it and refresh your web page it will give you true okay and if you would have initialize it by false then it will give you false so refresh your webpage and it gives false here okay now let's see how we can declare our string in JavaScript so once again gave var and then give the name to your variable for example my string and then is equal to and in double quotes whatever you write in between the double quote it will be your string right so for example my name is whatever right and just passed this variable in the alert function and save it and refresh your webpage and it gives you this string value here okay now for example you want to display a string with some double quotation or something so for example something like this okay and this will give you the error so when I save it and when i refresh my web page it gives me nothing and it gives me the syntax error and it says missing semicolon before statement okay and you can even go to this error by clicking here on the right hand side and it shows me this line okay so this error is coming from this line we know right and why this error is coming because these double quotes you cannot use inside your string variable so you can you know add any string inside your double quotes at the left hand side and the right hand side but whenever you try to include these double quote inside a string it will give you an error and it's obvious because javascript will think that this string starts here and ends here okay and whatever comes after it doesn't know what is it so to you know avoid this error what you can do is you can use a operator called escape operator okay and you can use it something like this so you use backslash just before your double quotation symbol so here and here and this means whatever comes after this double slash it will be considered as a part of the string okay so save it and refresh your webpage and now it will give you the answer so my and in this quotation and then the name okay something like this now there is a keyword in JavaScript which is called the type of and this keyword whatever you will write after this for example I'm writing this variable name my string after this type of keywords separated by one space then it will give you the type of this keyword okay so let's save it and let's see what it returns so I will refresh it and give me string so this keyword gives you the datatype of your variable okay so you can check what this variable is so my bool and save it and refresh your webpage once again and it will give you boolean okay and once again I will pass this my number here and save my code and refresh my webpage and it gives me this number so you can find the data type of a variable or some value using this type of keyword so in this way you can use data types in JavaScript I hope you enjoyed this blog
0 Comments