Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path025-string.js
More file actions
Latest commit
79 lines (59 loc) · 1.84 KB
/
Copy path025-string.js
File metadata and controls
79 lines (59 loc) · 1.84 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
// 1: WHAT ARE STRINGS?
"Hello"
'JavaScript'
"A"
"12345"
"Hello, my name is John and I'm learning JavaScript!"
// let str1 = "Hello World";
// let str2 = 'Hello World';
// let str3 = `Hello World`;
// 2: STRING LITERALS
letfirstName="John";
letlastName='Doe';
letgreeting=`Hello there!`;
letcity="New York";
letcountry='USA';
letmessage="Welcome to JavaScript programming!";
letemptyString="";
// 3: STRING CONSTRUCTOR
// let str1 = new String("Hello");
// let str2 = new String('World');
letliteral="Hello";
letconstructor=newString("Hello");
console.log(typeofliteral);// "string"
console.log(typeofconstructor);// "object"
letstr1="Hello";
letstr2="Hello";
letstr3=newString("Hello");
letstr4=newString("Hello");
console.log(str1===str2);// true
console.log(str3===str4);// false
// 4: WHY USE LITERALS?
// 5: PRIMITIVE WRAPPER
// let name = "John";
// console.log(name.toUpperCase()); // "JOHN"
// What you write:
// let name = "John";
// name.toUpperCase();
// // What JavaScript does behind the scenes:
// let name = "John";
// (new String(name)).toUpperCase();
// 6: PRACTICAL EXAMPLES
// String literals (recommended)
letusername="coder123";
letemail='user@example.com';
letbio=`JavaScript developer learning every day`;
// These are all string primitives
console.log(typeofusername);// "string"
console.log(typeofemail);// "string"
console.log(typeofbio);// "string"
// We can still use methods on them
console.log(username.length);// 8
console.log(email.toUpperCase());// "USER@EXAMPLE.COM"
console.log(bio.includes("JavaScript"));// true
// Don't do this
letwrongWay=newString("Hello");
console.log(typeofwrongWay);// "object" - not what we want!
// Do this instead
letrightWay="Hello";
console.log(typeofrightWay);// "string" - perfect!