Hey Kodlayıcılar!
JavaScript, Web Geliştirmede yaygın olarak kullanılan dillerden biridir. JS çevresinde, uygulamaları daha hızlı geliştirmenize yardımcı olan tonlarca çerçeve ve kitaplık sunan geniş ve hayati bir ekosistem gelişti. Aynı zamanda kodlama deneyimimizi biraz karmaşık hale getirdi. Bazen bir adım geri atmak ve kitaplık olmadan işlerin nasıl yapıldığını anlamaya çalışmak iyidir. Bu yazıda, her gün ortaya çıkan bu tekrarlayan, basit görevlerde biraz yardım sağlayacak bazı JS snippet'lerini inceleyeceğiz.
//strings
const names = ["Seema", "Rekha", "Jaya"];
names.sort();
//['Jaya', 'Rekha', 'Seema' ]
//Numbers
const numbers = [101, 8, 87];
numbers.sort((a, b) => {
return a - b;
});
//[ 8, 87, 101 ]
const items = ["Ball", "Bat", "Cup"]
const randomIndex = Math.floor(Math.random()*items.length)
items[randomIndex]
function reverseString(string) {
return string.split(" ").reverse().join(" ")
}
reverseString("Random String")
const element = document.querySelector("#element")
element.classList.contains("active")
const cars = ["Ford", "BMW", "Audi" ]
for (let car of cars) {
console.log(car)
}
/*
Ford
BMW
Audi
*/
const date = new Date()
const currentTime =
`${date.getHours()}:${date.getMintues()}:${date.getSeconds()}`
console.log(currentTimes)
//example output: "22:16:41"
const string = "You are awesome."
const replacedString = string.replace("You", "We")
console.log(replacedString) //Output: "We are awesome"
let profile = ['bob', 34, 'carpenter'];
let [name, age, job] = profile;
console.log(name);
// bob
let data = [1,2,3,4,5];
console.log(...data);
// 1 2 3 4 5
let data2 = [6,7,8,9,10];
let combined = [...data, ...data2];
console.log(...combined);
// 1 2 3 4 5 6 7 8 9 10
console.log(Math.max(...combined));
// 10
const maxElement = ( array, number = 1 ) => [...array].sort(( x,y ) => y - x).slice(0, number);
//Example
maxElement ([ 1,2,3,4,5]); // [5]
maxElement([ 6, 7, 8, 9 , 10, 10] , 2); // [10,10]
const name = "Jaya"
console.log(`Hi, ${name}. You have ${2 ** 3} new notifications.`}
//Hi, Jaya. You have 8 new notifications.