diff --git a/README.md b/README.md index 3bccb6e..1d259dc 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,3 @@ * Search for snippets that suit your needs. You can search by name, tag, language or using a snippet's description. Just start typing a term and see what comes up. * Click on each snippet card to view the whole snippet, including code, explanation and examples. -* You can copy code blocks on any snippet card, using the copy button at the top right. -* If you like the project, give it a star. It means a lot to the people maintaining it. diff --git a/javascript-number-methods.js b/javascript-number-methods.js new file mode 100644 index 0000000..9d7f66c --- /dev/null +++ b/javascript-number-methods.js @@ -0,0 +1,121 @@ +//Number Methods + +//Number(), + , parseInt(), parseFloat() + +Console.log(Number("123")); // return 123 (convert string to number) +console.log(+"123"); // return 123 (convert string to number) +console.log(parseInt("123")); // return 123 (convert string to number) +console.log(parseInt("12.34")); // return 12 +console.log(parseFloat("12.34")); // return 12.34 (convert string to number) + +___________________________________________________________________________________________________________________________________________________________________________ +//toString() +const nbr = 15 +console.log(nbr.toString()) // return 15 + +___________________________________________________________________________________________________________________________________________________________________________ +//toFixed() +const n1 = 214.45 +console.log(n1.toFixed(1));// "214.4" +console.log(n1.toFixed(4));// "214.4500" +console.log(+n1.toFixed(4));// 214.4500 + +___________________________________________________________________________________________________________________________________________________________________________ +//toPrecision() +let num = 3.14159; +let formattedNum = num.toPrecision(4); // '3.142' +let integerNum = 42; +let formattedInt = integerNum.toPrecision(4); // '42.00' + +___________________________________________________________________________________________________________________________________________________________________________ +//toLocaleString() +const n2 = 1800000; +console.log(n2.toLocaleString());// return "1.800.000" +console.log(n1.toLocaleString("tr-TR"));// return "1.800.000" + +___________________________________________________________________________________________________________________________________________________________________________ +//Number.isInteger() +console.log(Number.isInteger(24)); // true +console.log(Number.isInteger(24.00)); // true +console.log(Number.isInteger(24.04)); // false +console.log(Number.isInteger([1,2,3])); // false +console.log(Number.isInteger("24")); // false + +___________________________________________________________________________________________________________________________________________________________________________ +//All Math Functions + +//Math.abs() +console.log(Math.abs(5)); // 5 +console.log(Math.abs(-5)); // 5 +console.log(Math.abs(0)); // 0 +console.log(Math.abs(-3.14)); // 3.14 +console.log(Math.abs("42")); // 42 +console.log(Math.abs("Hello")); // NaN + +___________________________________________________________________________________________________________________________________________________________________________ +//Math.ceil() +console.log(Math.ceil(0.95)); // Expected output: 1 +console.log(Math.ceil(4)); // Expected output: 4 +console.log(Math.ceil(7.004)); // Expected output: 8 +console.log(Math.ceil(-7.004)); // Expected output: -7 + +___________________________________________________________________________________________________________________________________________________________________________ +//Math.floor() +console.log(Math.floor(5.95));// Expected output: 5 +console.log(Math.floor(5.05));// Expected output: 5 +console.log(Math.floor(5));// Expected output: 5 +console.log(Math.floor(-5.05));// Expected output: -6 + +___________________________________________________________________________________________________________________________________________________________________________ +//Math.max() +console.log(Math.max(5, 10, 15, 20)); // 20 +console.log(Math.max(-5, 3, -10, 8)); // 8 +console.log(Math.max(0, 0, 0, 0)); // 0 +console.log(Math.max(3.14, 2.71, 1.618)); // 3.14 +console.log(Math.max("42", "100", "7")); // 100 +console.log(Math.max(10, "Hello", -8)); // 10 +console.log(Math.max()); // -Infinity + +___________________________________________________________________________________________________________________________________________________________________________ +//Math.min() +console.log(Math.min(5, 10, 15, 20)); // 5 +console.log(Math.min(-5, 3, -10, 8)); // -10 +console.log(Math.min(0, 0, 0, 0)); // 0 +console.log(Math.min(3.14, 2.71, 1.618)); // 1.618 +console.log(Math.min("42", "100", "7")); // 7 +console.log(Math.min(10, "Hello", -8)); // -8 +console.log(Math.min()); // Infinity + +___________________________________________________________________________________________________________________________________________________________________________ +//Math.pow() +console.log(Math.pow(2, 3)); // 8 - 2^3 = 2 * 2 * 2 = 8 +console.log(Math.pow(5, 2)); // 25 - 5^2 = 5 * 5 = 25 +console.log(Math.pow(10, 0)); // 1 +console.log(Math.pow(3, -2)); // 0.1111111111111111 - 3^(-2) = 1 / (3^2) = 1 / 9 = 0.1111111111111111 +console.log(Math.pow(4, 0.5)); // 2 - 4^(0.5) = Karekök(4) = 2 + +___________________________________________________________________________________________________________________________________________________________________________ +//Math.random() +const randomNumber = Math.random(); +console.log(randomNumber); // 0.7267456282563128 +const randomBetween0And10 = Math.random() * 10; +console.log(randomBetween0And10); // 7.267456282563128 +const randomIntBetween1And100 = Math.floor(Math.random() * 100) + 1; +console.log(randomIntBetween1And100); // 42 + +___________________________________________________________________________________________________________________________________________________________________________ +//Math.round() +console.log(Math.round(3.2)); // 3 +console.log(Math.round(6.8)); // 7 +console.log(Math.round(1.5)); // 2 +console.log(Math.round(2.4)); // 2 +console.log(Math.round(-3.7)); // -4 + +___________________________________________________________________________________________________________________________________________________________________________ +//Math.sing() +console.log(Math.sign(5)); // 1 +console.log(Math.sign(-5)); // -1 +console.log(Math.sign(0)); // 0 +console.log(Math.sign("42")); // 1 +console.log(Math.sign("-3.14")); // -1 +console.log(Math.sign("Hello")); // NaN \ No newline at end of file diff --git a/javascript-objects-methods.js b/javascript-objects-methods.js new file mode 100644 index 0000000..a4d3bb3 --- /dev/null +++ b/javascript-objects-methods.js @@ -0,0 +1,200 @@ +//Object Methods + +/*In JavaScript, an "object" is a data structure that groups multiple values ​​or properties and facilitates access to those properties. +Objects contain named property-value pairs and can also contain methods such as functions. +This forms the basis of object-oriented programming and allows you to keep data in a more organized and structured way.*/ + +//Object create a function in +const person = { + name: "John", + greet: function () { + console.log("hello"); + }, +}; + +___________________________________________________________________________________________________________________________________________________________________________ +//Object assigning value from outside +let student = {}; +student.name = "John"; +student.greet = function () { + console.log("hello"); +}; +student.greet(); // hello + +___________________________________________________________________________________________________________________________________________________________________________ +//delete +//It allows us to delete a value inside the object. +const user = { + username: "AzureDiamond", + password: "hunter2", +}; +delete user.username; +console.log(user); // {"password": "hunter2"} + +___________________________________________________________________________________________________________________________________________________________________________ +//this keyword +//We can use the this keyword to access a property of an object from within a method of the same object. +const person1 = { + name: "John", + age: 30, + + // accessing name property by using this.name + greet: function () { + console.log("The name is" + " " + this.name); + }, +}; +person1.greet(); + +___________________________________________________________________________________________________________________________________________________________________________ +//Also, when accessing values ​​defined inside the function without using this, we use the this keyword to refer to the values ​​inside the object. +const person2 = { + name: "John", + surname: "Deep", + greet: function () { + let surname = "Doe"; + console.log("The name is" + " " + this.name + " " + surname); + }, + greet2: function () { + let surname = "Doe"; + console.log("The name is" + " " + this.name + " " + this.surname); + }, +}; +person2.greet(); // "The name is John Doe" +person2.greet2(); // "The name is John Deep" + +___________________________________________________________________________________________________________________________________________________________________________ +//Object.keys +//Creates an array containing the keys of an object. +const employees = { + boss: "Michael", + secretary: "Pam", + sales: "Jim", + accountant: "Oscar", +}; +console.log(Object.keys(employees)); // ["boss", "secretary", "sales", "accountant"] + +___________________________________________________________________________________________________________________________________________________________________________ +// Object.values +//Creates an array containing the values of an object. +const session = { + id: 1, + time: `26-July-2018`, + device: "mobile", + browser: "Chrome", +}; +console.log(Object.values(session)); // [1, "26-July-2018", "mobile", "Chrome"] + +const operatingSystem = { + name: "Ubuntu", + version: 18.04, + license: "Open Source", +}; + +console.log(Object.entries(operatingSystem)); +// output : +`[["name", "Ubuntu"][("version", 18.04)][("license", "Open Source")]];` + +___________________________________________________________________________________________________________________________________________________________________________ +const operatingSystem1 = { + name: "Ubuntu", + version: 18.04, + license: "Open Source", +}; +for (let [key, value] of Object.entries(operatingSystem1)) { + console.log(key + " = " + value); +} +// output : +("name = Ubuntu"); +("version = 18.04"); +("license = Open Source"); + +___________________________________________________________________________________________________________________________________________________________________________ +//Object.assign() +//Used to combine values ​​of multiple objects +const name1 = { + firstName: "Philip", + lastName: "Fry", +}; + +const details1 = { + job: "Delivery Boy", + employer: "Planet Express", +}; +console.log(Object.assign(name1, details1)); +// output : +`{firstName: "Philip", lastName: "Fry", job: "Delivery Boy", employer: "Planet Express"}`; + +___________________________________________________________________________________________________________________________________________________________________________ +//It is also possible to combine with the spread. +const name2 = { + firstName: "Philip", + lastName: "Fry", +}; + +const details = { + job: "Delivery Boy", + employer: "Planet Express", +}; +const character = { ...name2, ...details }; + +___________________________________________________________________________________________________________________________________________________________________________ +//Object.freeze() +//Prevents changing, deleting and adding values ​​in the specified object. +const user2 = { + username: "AzureDiamond", + password: "hunter2", +}; + +const newUser = Object.freeze(user2); + +newUser.password = "*******"; +newUser.active = true; +console.log(newUser); // {username: "AzureDiamond", password: "hunter2"} + +___________________________________________________________________________________________________________________________________________________________________________ +//Object.seal() +//Allows the values ​​in the specified object to be changed, but prevents adding and deleting values. +const user3 = { + username: "AzureDiamond", + password: "hunter2", +}; + +const newUser = Object.seal(user3); +newUser.password = "*******"; +newUser.active = true; //eklenmez +delete newUser.username; //silinmez +console.log(newUser); // {username: "AzureDiamond", password: "*******"} + +___________________________________________________________________________________________________________________________________________________________________________ +//shorthand +//We can define your object methods in a more practical way without assigning keywords. +// We assigned a function to greet and now we can call this function as greet. +let person3 = { + firstName: "John", + lastName: "Doe", + greet: function () { + console.log("Hello, World!"); + }, +}; +person3.greet(); // "Hello, World!"; + +___________________________________________________________________________________________________________________________________________________________________________ +// when we create the function directly without defining a keyword +// The function name and keyword are automatically created inside +let person5 = { + firstName: "John", + lastName: "Doe", + greet() { + console.log("Hello, World!"); + }, +}; +person5.greet(); //Hello, World!" + +___________________________________________________________________________________________________________________________________________________________________________ +//Object.getPrototypeOf() +//Object.getPrototypeOf() is used to get the internal hidden [[Prototype]] of an object, also accessible through the __proto__ property. +const employees1 = ["Ron", "April", "Andy", "Leslie"]; +Object.getPrototypeOf(employees1); +//Output +//[constructor: ƒ, concat: ƒ, find: ƒ, findIndex: ƒ, pop: ƒ, …] +