Basic JavaScript Cheet Sheet
Hello Everyone,
this topic is to teach you the basics of Javascript, which is the language used in Lens Studio Scripting.
Variables
var a; // variable var b = "init"; // string var c = "Hi" + " " + "Joe"; // = "Hi Joe" var d = 1 + 2 + "3"; // = "33" // the third number is string var e = [2,3,5,8]; // array var f = false; // boolean var h = function(){}; // function object const PI = 3.14; // constant (it will not change)
var age = 18; // number var name = "Jane"; // string var name = {first:"Jane", last:"Doe"}; // object var truth = false; // boolean var sheets = ["HTML","CSS","JS"]; // array
Printing
print("text") // this will print " text " to the output area
Functions
function functionName(){
}
// you can call the function by
functionName()
If conditions
var a = 1
var b = 2
if (a > b) {
print("A is bigger than B")
} else {
print("B is bigger than A")
}
Math
var pi = Math.PI; // 3.141592653589793 Math.round(4.4); // = 4 - rounded Math.round(4.5); // = 5 Math.pow(2,8); // = 256 - 2 to the power of 8 Math.sqrt(49); // = 7 - square root Math.abs(-3.14); // = 3.14 - absolute, positive value Math.ceil(3.14); // = 4 - rounded up Math.floor(3.99); // = 3 - rounded down Math.sin(0); // = 0 - sine Math.cos(Math.PI); // OTHERS: tan,atan,asin,acos, Math.min(0, 3, -2, 2); // = -2 - the lowest value Math.max(0, 3, -2, 2); // = 3 - the highest value Math.log(1); // = 0 natural logarithm Math.exp(1); // = 2.7182pow(E,x) Math.random(); // random number between 0 and 1 Math.floor(Math.random() * 5) + 1; // random integer, from 1 to 5
Stings
var abc = "abcdefghijklmnopqrstuvwxyz"; var esc = 'I don\'t \n know'; // \n new line var len = abc.length; // string length abc.indexOf("lmno"); // find substring, -1 if doesn't contain abc.lastIndexOf("lmno"); // last occurance abc.slice(3, 6); // cuts out "def", negative values count from behind abc.replace("abc","123"); // find and replace, takes regular expressions abc.toUpperCase(); // convert to upper case abc.toLowerCase(); // convert to lower case abc.concat(" ", str2); // abc + " " + str2 abc.charAt(2); // character at index: "c" abc[2]; // unsafe, abc[2] = "C" doesn't work abc.charCodeAt(2); // character code at index: "c" -> 99 abc.split(","); // splitting a string on commas gives an array abc.split(""); // splitting on characters
this is the basic things that most new people to scripting need, if require any further information please tell me to update the topic
This is going to be helpful thanks
Thanks for sharing this info!