-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01Basic.js
More file actions
143 lines (115 loc) · 5.37 KB
/
01Basic.js
File metadata and controls
143 lines (115 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
let name="vashisht";
var name1="vashisht kumar"
const name3="vashist kumar";
//console.log(name,name1,name3);// vashisht vashisht kumar vashisht kumar
//console.table([name,name1,name3]);//in form of table starting with index 0;
name="vashisht changed the name";
name1="vashisht kumar changeed the name1";
//name3="vashisht kumar not able to change the name";//throw error as it is constant;
let phoneNum;
//console.log(phoneNum);//undefined
/*:::::::::::::::::::::::::::::::::::::::::JavaScript Identifiers:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
All JavaScript variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
The general rules for constructing names for variables (unique identifiers) are:
1 Names can contain letters, digits, underscores, and dollar signs.
2 Names must begin with a letter.
3 Names can also begin with $ and _ (but we will not use it in this tutorial).
4 Names are case sensitive (y and Y are different variables).
5 Reserved words (like JavaScript keywords) cannot be used as names.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
let person = "vashisht", carName = "scorpio", price = "16lakh";
//console.log(person,price,carName);// vashisht scorpio 16lakh
/*__________________________________________________________________________
you can redeclare var
var name3;
but can't redeclare let and const
___________________________________________________________________________*/
/*::::::::::::::::::::string concatination:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
let ap="vashisht"+" "+"learning"+"javascript";
console.log(ap);// vashisht leraning javascript
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:::::::::::::::::::Some examples of string concatination:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
let a=6+9+"8";//158
let b="9"+8+8;//988
console.table([a,b]);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::::::::::::::::::Points to remember for let:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1 Variables defined with let cannot be Redeclared but i case of var you can
{let x = "vashisht";
let x = 0;} not possible throw error
{var x = "vashisht";
var x = 0;} it will not throw error x will updated with value 0;
2 Variables defined with let must be Declared before use
3 Variables defined with let have Block Scope
/////////////////////////////////////////////////
{
let x = 2;
}
// x can NOT be used here
/////////////////////////////////////////////////////
-------------------------------------------------------
{
var x = 2;
}
// x CAN be used here
-------------------------------------------------------------
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:::::::::::::::::::::::::::::::::::Hoisting:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Hoisting-only can be used for var not for const and let
examples
car="scorpio";
console.log(car)//scorpio
var car
//////////////////////
but in case of let it will throw reference error
car="scorpio"
console.log(car)
let car;
////////////////
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:::::::::::::::::::::::::::::::::CONST:::::::::::::::::::::::::::::::::::::::::::::
const PI = 3.141592653589793;
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
JavaScript const variables must be assigned a value when they are declared.
When to use JavaScript const?
Always declare a variable with const when you know that the value should not be changed.
Use const when you declare:
A new Array
A new Object
A new Function
A new RegExp
/////////////////////////////////////////////////////////////
Constant Objects and Arrays
The keyword const is a little misleading.
It does not define a constant value. It defines a constant reference to a value.
Because of this you can NOT:
Reassign a constant value
Reassign a constant array
Reassign a constant object
But you CAN:
Change the elements of constant array
Change the properties of constant object
///////////////////////////////////////
examples
1.const mobile=["poco", "samsung", "nokia"];
mobile.push("redmi");
//console.log(mobile);// poco samsung nokia redmi
mobile[0] = "apple";;
console.log(mobile)// apple samsung nokia
2
mobile=["infinix","nothing","hawai"];
console.log(mobile);//it will throw error assigment to const variable
3// You can create a const object:
const car = {type:"Fiat", model:"500", color:"white"};
// You can change a property:
car.color = "red";
// You can add a property:
car.owner = "vashisht";
console.log(car);
4
const car = {type:"Fiat", model:"500", color:"white"};
car = {type:"Volvo", model:"EX60", color:"red"}; // ERROR // you cant change the whole object
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/