|
| 1 | +/* Foundations of js - 01 -> PRIME FOCUS = basics of variables and datatypes*/ |
| 2 | + |
| 3 | +/* |
| 4 | +1.What are value |
| 5 | +2.What is a variable |
| 6 | +3.what are datatypes |
| 7 | +4.what are 7 primitive datatypes |
| 8 | +5.3 ways to Declare a variable in js - let,const,var |
| 9 | +6.console.log function |
| 10 | +7.Type of operator |
| 11 | +8.Whats Dynamic typing & its action |
| 12 | +9.Comments in JS |
| 13 | +*/ |
| 14 | + |
| 15 | +// 1. What are values - its a piece of data - 13 , 12.89 , true , 'Indresh' etc |
| 16 | + |
| 17 | +// 2. What is a variable - All tasks in software deals with data and thus it is essential to store it. Variables are containers that can store values/data |
| 18 | + |
| 19 | +// 3. What is a datatype - values can be of different types. These are called datatypes. In JS values are of broadly 2 types -> primitive datatypes and objects |
| 20 | + |
| 21 | +// 4. what are 7 primitive datatypes |
| 22 | +/* |
| 23 | + -> Numbers = both int and floats |
| 24 | + -> String = Sequence of characters in quotes |
| 25 | + -> Boolean = logical datatype whose values are - true or false |
| 26 | + -> undefined = means empty value & If a variable is declared and not yet initialised it takes up this datatype as default |
| 27 | + -> null = indicates empty/no value and is a assigned value intentionally |
| 28 | + -> Bigint = ES2020 feature - stores large numbers |
| 29 | + -> Symbol = ES2015 feature - represents values that are unique and cant be changed |
| 30 | + */ |
| 31 | + |
| 32 | +// 5. Declaring and initialising a variable |
| 33 | +/* declaring Syntax-> keyword variablename ; |
| 34 | + initialising Synatx -> variablename = value; |
| 35 | +
|
| 36 | + There are 3 keywords to declare a variable |
| 37 | + 1.let keyword -> used to declare those variables whose values may be mutated in future |
| 38 | + 2.const keyword -> used to declare those variables whose values are not going to be changed/reassigned/mutated |
| 39 | + 3.var keyword -> shd be completely avoided after ES6 |
| 40 | + */ |
| 41 | + let num1; //declaring |
| 42 | + num1 = 10; //initialising |
| 43 | + let num2 = 12; //declaring and initialing at the same time |
| 44 | + num1 = 15; //reassigning/mutating/changing the value of variable num1 |
| 45 | + |
| 46 | + const fullname = "Indresh"; //declaring and initialising shd be done at the same time for const keyword |
| 47 | + //const class; This gives error becz const variables cant be left without initialising |
| 48 | + //Its preffered to keep most of your code in const if you are sure its not going to be mutated |
| 49 | + |
| 50 | + var a; |
| 51 | + a = 10; |
| 52 | + var b = 12; |
| 53 | + //by this we see var is similar to let but there are some differences -> |
| 54 | + /*Variables declared by let are only available inside the block where they're defined. Variables declared by var are available throughout the function in which they're declared*/ |
| 55 | + |
| 56 | + // 6. console.log function |
| 57 | + /* |
| 58 | + -> It is a built-in function in js which when called prints/logs the contents in the console of the browser |
| 59 | + */ |
| 60 | + console.log("Welcome javascript"); //logging a string |
| 61 | + console.log(num1); //logging a variable |
| 62 | + console.log(12); //logging a number |
| 63 | + console.log(15 + 6); //logging an expression |
| 64 | + console.log("hello " + fullname); //logging a concatenated string and variable |
| 65 | + |
| 66 | + // 7. Typeof operator |
| 67 | + |
| 68 | + //It tells us the datatype of a value/variable |
| 69 | + console.log(typeof num1); |
| 70 | + console.log(typeof fullname); |
| 71 | + console.log(typeof true); |
| 72 | + let x; |
| 73 | + console.log(typeof x); // uninitialised variable - undefined datatype |
| 74 | + let y = null; |
| 75 | + console.log(typeof y); // gives output as object which is a bug and not resolved for legacy reason |
| 76 | + |
| 77 | + // 8. Dynamic typing in javascript |
| 78 | + /* |
| 79 | + This means we dont havre to manually define the datatype of the value which the variable can store. |
| 80 | + a variable can have any value belonging to any datatype |
| 81 | + */ |
| 82 | + let dynamic = 10; //dynamic holds a num |
| 83 | + dynamic = true; //dynamic holds a boolean |
| 84 | + dynamic = "hello JS"; //dynamic holds a string |
| 85 | + |
| 86 | + // 9. Comments in JS |
| 87 | + |
| 88 | + /* multiline comment */ |
| 89 | + // single line comment |
0 commit comments