Skip to content

Latest commit

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Learning JavaScript

Console.log()

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;");

Variables

There are (3) keywords to declare variables:

  • var the most lenient type of Variables.
  • let included in ES6 somewhat strict type of variables
  • const used to create constant (unmodifiable variables)

Variables

There are (3) ways to create a variable in JS:

  • var: creates a somewhat global variable.
  • let: creates a scoped variable
  • const: 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 & Non-Primitive Data Types

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

Using dot (.) operator:

letname=_myObj.name;console.log(name);// output: "ARK"

Using Object Literal:

letage=_myObj["age"];console.log(age);// Output: 19

String Concatenation

Concatenation is the process used to combine two or more string together. There are severel ways of concatenating String in JavaScript

  1. using the concat method
  2. using the + operator
  3. using the , operator

Method 1:

str1="My";str2="String";// combines the two existing stringsstr3=str1.concat(str2);// MyString

Method 2:

str1="My name is";str2="ARK";console.log(str1+str2);// My name isARK

Method 3:

str1="My name is";str2="ARK";console.log(str1,str2);

Loops

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...

String Methods

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'

Arrays

A data type that is used to store collection of data types.

letarr=["banana",70,3.142,true];

Methods of Array

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 front

Turnary Operator

Syntax:

condition ? if_true : if_false;

Example:

letresult=1>2 ? "1 is greater than 2" : "1 is NOT greater than 2";console.log(result);

About

Learning JavaScript by coding. The following branch contains codes (in JavaScript language) for each of the programming concept

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Used by

Contributors

Languages