- Getting Started
- JS in the Web Browser
console.log()- Variables
- Data Types
- String Concatenation
- Operators
- Conditional Statements
This is the method used to output text onto the console (not the screen)
console.log("Hello World!");//semi-colons are optional// you can format the code in Console bu using `%c`console.log("%c Hi There!","color:blue; font-size:48px;");There are (3) keywords to declare variables:
varthe most lenient type of Variables.letincluded in ES6 somewhat strict type of variablesconstused to create constant (unmodifiable variables)
There are (3) ways to create a variable in JS:
var: creates a somewhat global variable.let: creates a scoped variableconst: creates a CONSTANT variable
/* Using let */let_var=67;functionmyFunc(){// this variables is only accessible inside the function.let_var=100;}/* Using const */// this variable can not be 'reassigned'const_constant="I am unchangeable";/* Using var */// This variable can be accessed EVERYWHERE in the codevar_anotherVar=null;Primitive: Data types that are built-in to the language. Examples are:
// nn bb ss ulet_null=null;let_num=34;let_bool=true;let_bigInt=456n;let_string="ARK";let_symbol=Symbol("I am a Symbol!");let_undef=undefined;Non-Primitive: Data Types that are user-defined. Examples are: Objects.
let_myObj={name: "ARK",age: 19,skinColor: "Dark Orange",};In the above code an object is assigned to the variable named '_myObj'. The object contains key-value pair this means every properties of object can be accessed by a specific key. There are several ways to access the properties of an object
letname=_myObj.name;console.log(name);// output: "ARK"letage=_myObj["age"];console.log(age);// Output: 19Concatenation is the process used to combine two or more string together. There are severel ways of concatenating String in JavaScript
- using the
concatmethod - using the
+operator - using the
,operator
Method 1:
str1="My";str2="String";// combines the two existing stringsstr3=str1.concat(str2);// MyStringMethod 2:
str1="My name is";str2="ARK";console.log(str1+str2);// My name isARKMethod 3:
str1="My name is";str2="ARK";console.log(str1,str2);There are many types of loops in JS:
For loop
for(leti=0;i<11;i++){console.log(i);}For-in loop:Used for printing the values of an object
// An object literal assigned to a variable named 'Obj'letobj={name: "ARK",age: 19,isMale: true,};for(letkeysinobj){console.log(`${keys}: ${obj[keys]}`);}/*Output: name: ARK age: 19 isMale: true */For-of loop:used to iterate over the elements of an Array
// Array of fruits (string).letarr=["lemon","banana","apple","strawberry"];for(letitemofarr){console.log(`${item}`);}/* Output: lemon banana apple strawberry */While Loop
leti=0;while(true){console.log(++i);}// Output: 1, 2, 3...letname="ark";console.log(name.length);// tells the number of charachters present in the stringconsole.log(name.toUpperCase());// converts the string to UPPERCASEconsole.log(name.toLowerCase());// converts the string to lowercaseconsole.log(name.replace("a","s"));// replaces 'a' in string 'ark' with 's'. Output: 'srk'A data type that is used to store collection of data types.
letarr=["banana",70,3.142,true];arr.push("ARK");// adds new element at the endarr.pop();// removes element from the endarr.shift();// removes element from the frontarr.unshift();// adds element from the frontSyntax:
condition ? if_true : if_false;Example:
letresult=1>2 ? "1 is greater than 2" : "1 is NOT greater than 2";console.log(result);