Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e2f9af3

Browse files
Number Methods and Symbol Added
1 parent 3b1462c commit e2f9af3

File tree

13 files changed

+260
-0
lines changed

13 files changed

+260
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let number = 10;
2+
3+
console.log(number.toExponential(1));
4+
// JavaScript Number toExponential()
5+
// The JavaScript Number toExponential() method returns a string representing the Number object in exponential notation.
6+
7+
// The syntax of the toExponential() method is:
8+
9+
// num.toExponential(fractionDigits)
10+
// Here, num is a number.
11+
12+
// Number toExponential() Parameters
13+
// The toExponential() method takes in:
14+
//
15+
// fractionDigits (Optional) - An integer specifying the number of digits after the decimal point. By default, it is as many digits as necessary to specify the number.
16+
// Return value from Number toExponential()
17+
// Returns a string representing the given Number object in exponential notation, rounded to fractionDigits digits after the decimal point.
18+
// Example: Using Number.toExponential()
19+
let num = 695.8457;
20+
21+
let exp_num = num.toExponential();
22+
console.log(exp_num); // 6.958457e+2
23+
24+
let exp_num1 = num.toExponential(2);
25+
console.log(exp_num1); // 6.96e+2
26+
// Run Code
27+
// Output
28+
29+
6.958457e+2
30+
6.96e+2

‎12. Symbol in JavaScript/1.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// JavaScript Symbol
2+
// The JavaScript ES6 introduced a new primitive data type called Symbol. Symbols are immutable (cannot be changed) and are unique. For example,
3+
4+
// two symbols with the same description
5+
6+
const value1 = Symbol('hello');
7+
const value2 = Symbol('hello');
8+
9+
console.log(value1 === value2); // false
10+
// Though value1 and value2 both contain the same description, they are different.

‎12. Symbol in JavaScript/2.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Creating Symbol
2+
// You use the Symbol() function to create a Symbol. For example,
3+
4+
// creating symbol
5+
const x = Symbol();
6+
7+
console.log(typeof x); // symbol
8+
// You can pass an optional string as its description. For example,
9+
10+
const x1 = Symbol("hey");
11+
console.log(x1); // Symbol(hey)

‎12. Symbol in JavaScript/3.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Access Symbol Description
2+
// To access the description of a symbol, we use the . operator. For example,
3+
4+
const x = Symbol('hey');
5+
console.log(x.description); // hey

‎12. Symbol in JavaScript/4.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Add Symbol as an Object Key
2+
// You can add symbols as a key in an object using square brackets []. For example,
3+
4+
let id = Symbol("id");
5+
6+
let person = {
7+
name: "Jack",
8+
9+
// adding symbol as a key
10+
[id]: 123 // not "id": 123
11+
};
12+
13+
console.log(person); // {name: "Jack", Symbol(id): 123}

‎12. Symbol in JavaScript/5.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Symbols are not included in for...in Loop
2+
// The for...in loop does not iterate over Symbolic properties. For example,
3+
4+
let id = Symbol("id");
5+
6+
let person = {
7+
name: "Jack",
8+
age: 25,
9+
[id]: 12
10+
};
11+
12+
// using for...in
13+
for (let key in person) {
14+
console.log(key);
15+
}

‎12. Symbol in JavaScript/6.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Benefit of Using Symbols in Object
2+
// If the same code snippet is used in various programs, then it is better to use Symbols in the object key. It's because you can use the same key name in different codes and avoid duplication issues. For example,
3+
4+
let person = {
5+
name: "Jack"
6+
};
7+
8+
// creating Symbol
9+
let id = Symbol("id");
10+
11+
// adding symbol as a key
12+
person[id] = 12;
13+
// In the above program, if the person object is also used by another program, then you wouldn't want to add a property that can be accessed or changed by another program. Hence by using Symbol, you create a unique property that you can use.
14+
15+
// Now, if the other program also needs to use a property named id, just add a Symbol named id and there won't be duplication issues. For example,
16+
person = {
17+
name: "Jack"
18+
};
19+
20+
let id1 = Symbol("id");
21+
22+
person[id1] = "Another value";
23+
// In the above program, even if the same name is used to store values, the Symbol data type will have a unique value.
24+
25+
// In the above program, if the string key was used, then the later program would have changed the value of the property. For example,
26+
27+
person = {
28+
name: "Jack"
29+
};
30+
31+
// using string as key
32+
person.id = 12;
33+
console.log(person.id); // 12
34+
35+
// Another program overwrites value
36+
person.id = 'Another value';
37+
console.log(person.id); // Another value
38+
// In the above program, the second user.id overwrites the previous value.
39+

‎12. Symbol in JavaScript/7.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Symbol Methods
2+
// There are various methods available with Symbol.
3+
4+
// Method Description
5+
// for() Searches for existing symbols
6+
// keyFor() Returns a shared symbol key from the global symbol registry.
7+
// toSource() Returns a string containing the source of the Symbol object
8+
// toString() Returns a string containing the description of the Symbol
9+
// valueOf() Returns the primitive value of the Symbol object.
10+
// Example: Symbol Methods
11+
// get symbol by name
12+
let sym = Symbol.for('hello');
13+
let sym1 = Symbol.for('id');
14+
15+
// get name by symbol
16+
console.log( Symbol.keyFor(sym) ); // hello
17+
console.log( Symbol.keyFor(sym1) ); // id

‎12. Symbol in JavaScript/8.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Symbol Properties
2+
// Properties Description
3+
// asyncIterator Returns the default AsyncIterator for an object
4+
// hasInstance Determines if a constructor object recognizes an object as its instance
5+
// isConcatSpreadable Indicates if an object should be flattened to its array elements
6+
// iterator Returns the default iterator for an object
7+
// match Matches against a string
8+
// matchAll Returns an iterator that yields matches of the regular expression against a string
9+
// replace Replaces matched substrings of a string
10+
// search Returns the index within a string that matches the regular expression
11+
// split Splits a string at the indices that match a regular expression
12+
// species Creates derived objects
13+
// toPrimitive Converts an object to a primitive value
14+
// toStringTag Gives the default description of an object
15+
// description Returns a string containing the description of the symbol
16+
// Example: Symbol Properties Example
17+
const x = Symbol('hey');
18+
19+
// description property
20+
console.log(x.description); // hey
21+
22+
const stringArray = ['a', 'b', 'c'];
23+
const numberArray = [1, 2, 3];
24+
25+
// isConcatSpreadable property
26+
numberArray[Symbol.isConcatSpreadable] = false;
27+
28+
let result = stringArray.concat(numberArray);
29+
console.log(result); // ["a", "b", "c", [1, 2, 3]]

‎Pratice/21.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let a = 1;
2+
let b = 2;
3+
let c = "1";
4+
let name = "Hello";
5+
let name1 = "Hello";
6+
let name2 = "hello";
7+
8+
console.log(a == b); // false
9+
console.log(a == c); // true
10+
console.log(name == name1); // true
11+
console.log(name == name2); // false
12+
13+
console.log(name === name1);
14+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /