JavaScript intermediate
{{lang-javascript}}function externalFunction(x) {
return function internalFunction(y) {
return x + y;
};
}
const add5 = externalFunction(5);
console.log(add5(3)); // Output: 8
{{lang-javascript}}class Animal {
constructor(name) {
this.name = name;
}
talk() {
console.log("generic sound");
}
}
class Dog extends Animal {
constructor(name) {
super(name);
}
talk() {
console.log("Bau!");
}
}
const jsonString = '{"name":"Bob","age":25,"city":"New York","interests":["music","sport"]}';
const jsonObj = JSON.parse(jsonString);
console.log(jsonObj );
// Output: { name: 'Bob', age: 25, city: 'New York', interests: [ 'music', 'sport' ] }
console.log(jsonObj.name); // Output: Bob
console.log(jsonObj.interests[0]); // Output: music
console.log(typeof jsonObj); // Output: object
let firstName = "John";
let lastName = "Doe";
let text = \`Welcome ${firstName}, ${lastName}!\`;
//Output: Welcome John, Doe!
{{lang-javascript}}function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log(`Hello, I'm ${this.name}`);
};
const person1 = new Person("Alice");
person1.greet(); // Output: Hello, I'm Alice
{{lang-javascript}}fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
const myObj= {
name: "Alice",
age: 30,
city: "Rome",
interests: ["programming", "travel"]
};
const JSONstring = JSON.stringify(myObj);
console.log(JSONstring );
// Output: {"name":"Alice","age":30,"city":"Rome","interests":["programming","travel"]}
console.log(typeof JSONstring); // Output: string
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.