change material of an object in javascript

hi , I have two materials in my materials folder called “mat1” and “mat2” . I have a cube which has “mat1” as it’s material . I want to change the material of my cube to mat2 when some event occurs . I have already searched so much and found nothing about how to access a material from materials folder . can anyone help me with this?
thanks

You should create the variable mat2 and assign it at the Inspector, then replace the material by this variable:

var mat2: Material; // assign the second material to this variable in the Inspector

    // whenever you want to change the material, just assign the new one:
    renderer.material = mat2;