js firstchild code example

Example 1: css first child

:first-child {
	//styles here
}

:nth-child(1) { //the benefit of this is you can do it for 2nd, 3rd etc...
	//styles here 
}

Example 2: first child element javascript

Both these will give you the first child node:

console.log(parentElement.firstChild); // or
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: javascript take first element of array

const array = [1,2,3,4,5];
const firstElement = array.shift();

// array -> [2,3,4,5]
// firstElement -> 1

Example 4: how to add first child using js

var eElement; // some E DOM instance
var newFirstElement; //element which should be first in E

eElement.insertBefore(newFirstElement, eElement.firstChild);