javascript first child code example
Example 1: css first child
:first-child {
}
:nth-child(1) {
}
Example 2: first child element javascript
Both these will give you the first child node:
console.log(parentElement.firstChild);
console.log(parentElement.childNodes[0]);
If you need the first child that is an element node then use:
console.log(parentElement.children[0]);
Example 3: how to add first child using js
var eElement;
var newFirstElement;
eElement.insertBefore(newFirstElement, eElement.firstChild);
Example 4: javascript find first element of array
let array = ["a", "b", "c", "d", "e"];
let [first1] = array;
let first2 = array[0];