JavaScript variables
Declaration of string variables in JavaScript
let name of variable="the string";
For example
let box="C language";
- For declaring a string it should be given only within the double quotes.
- Now to print the string from the variable follow the below step,
console.log(box);
Output:
C language
The above statement should be followed to print "C language".
If you see we have used the variable name "box" to declare the string because the string has already declared in the variable name "box". So, instead of using the string use the variable name to print the string.
Declaration of number variable in JavaScript
let number =100;
- For declaring a number don't use double quotes, if double quotes is used it becomes a string.
- We can perform Arithmetic operations by just declaring them and performing
For example
let firstNum = 100;
let secondNum 2 =200;
console.log(firstNum+secondNum);
Output:
300
Note: If you see the above example I had used the variable names with two parts like first and Num when we use two names we first use small letter for the first name and capital letter for second name.
Here we had performed addition operation.
If you see the printing statement we had not used double quotes because as we had already declared them in variable we need not use double quotes to print them. If double quotes is used them it just print the statement that is given within the quotes it doesn't perform operation.
It was very useful
ReplyDelete