Using Javascript to Create Dynamic, Data-Driven Web Pages

This week we get a brief introduction to the Javascript language and how to use it within webpages.

Javascript Variables

Javascript Arrays and Objects

var newArr = ['I', 'am', 'finally', 'learning', 'a', 'programming', 'language'];

// console.log(newArr[0]) will output the string 'I'

// document.write(newArr[4]) will output 'a'
newArr.push('Wow!');

console.log(newArr);

// output >> ["I", "am", "finally", "learning", "a", "programming", "language", "Wow!"]

newArr.unshift('Guess what?');

console.log(newArr);

// output >> ["Guess what?", "I", "am", "finally", "learning", "a", "programming", "language", "Wow!"]
var Arr1 = [1, 2, 3, 4, 5];

var hodler1 = Arr1.pop();

console.log(hodler1);

// output >> 5

console.log(Arr1);

// output >> [1, 2, 3, 4]

var Arr2 = [];

Arr2.push(Arr1.shift());

console.log(Arr2);

// output >> [1]

console.log(Arr1);

// output >> [2, 3, 4]
var kizito = {
  gender: 'Male',
  age: 21,
  nationality: 'Nigerian',
  colors: ['Blue', 'Purple', 'Green'],
};

console.log(kizito.age);

// output >> 21

Javascript Control Structures

Javascript Functions


function newFunc (arg1, arg2, ... argX {

. . .

// Code goes here

. . .

};

Javascript Regular Expressions

var name = 'My Name';

console.log(name.charAt(3));

// output >> N

On Regular Expressions:

var phoneNum = '2348131132744';

// A regex checking if the num contains 234

/234/.test(phoneNum);

// output >> true
var someString = 'gh48bdsnew9d84n';

// test for a digit-letter-digit character sequence within the string

/[0-9][a-z][0-9]/.test(someString);

// output >> true

someString.search(/[0-9][a-z][0-9]/); // second format

// output >> 10