Tommi Space

JavaScript

I am stacking here everything I find out on the web which might be useful for when I am finally going to get into the magical universe of JavaScript.

Resources

Node

  • oclif: The Open CLI Framework · Create command line tools your users love

Notes

  • When combining stuff, the string always wins
  • you can grab the content of a DOM Element by selecting it and using innerText
  • textContent is better than innerText

Reading and writing JSON

To read a JSON file in JavaScript, you can use the fetch() method to get the contents of the file as a Response object. Then, you can convert the Response object to JSON using the json() method.

Here is an example:

fetch('example.json')
	.then(response => response.json())
	.then(data => console.log(data))
	.catch(error => console.error(error))

To write a JSON file in JavaScript, you can create an object that you want to write to a file, convert it to a JSON string using the JSON.stringify() method, and then write the string to a file using the fs module.

Here is an example:

const fs = require('fs')

const data = {
	name: 'John',
	age: 30,
	city: 'New York'
}

const jsonData = JSON.stringify(data)

fs.writeFile('example.json', jsonData, err => {
	if (err) {
		console.error(err)
		return
	}
console.log('Data written to file')
})

[JavaScript]: https://en.wikipedia.org/wiki/JavaScript ‘JavaScript on Wikipedia’

🔎