Basics of JavaScript

How to print a simple statement

console.log('Hello World.'); //How to print "Hello World"
Hello World.

How to declare variables in JavaScript

  • You can declare a variable using the two statements below
  • NOTE: JavaScript is case sensitive like Java
let newVariable;
myVariable = 'Shraddha';
  • You can also declare a variable with one line of code
let newVariable = 'Shraddha';

How to declare different types of variables

  • A string variable is used for a value, like a word
    • Make sure to include single quotation marks
  • A number variable can be used by simply declaring the variable equal to the value of the number
  • Booleans, like in java, can be used by letting the variable equal either true or false
  • An Array can be used to store multiple values in one
  • You can store an object as a variable in JavaScript, which is a little different from in Java
let newString = 'Shraddha'; //String variable
let newNumber = 5; //Number variable
let newBoolean = true; //Boolean variable
let newArray = [1, 'Shraddha'. 5]; //Array variable
let newObject = document.querySelector('a1'); //Object as a variable

iJavaScript $$.async()

  • This is used to request synchronous code. This will then evaluate the code and be ready to get more requests
$$.async();

console.log("Hello, World!");

setTimeout($$.done, 1000)
Hello, World!
  • You can aso add a timer to print a line of code after a while
$$.async();

console.log("Hello, World!"); //How to print "Hello World" wih JavaScript

var action = {
    $$: $$,
    console: console,
};

setTimeout(function() {
    $$.clear(0);    // clear output cell
    action.$$.sendResult("Goodbye!");
}, 5000);  // 5 second timer
'Goodbye!'

Building an Array with my Scrumtean

// define a student Array of Person(s)
var students = [ 
    new Person("Shraddha", "ftc13184-SK", 2023),
    new Person("Pravavi", "PranaviInukurti", 2023),
    new Person("Madhumita", "mnarayan", 2023),
    new Person("Meena", "MAnn, 2023),
    new Person("Rebecca", "Rebecca-123", 2023)
];

// define a classroom and build Classroom objects and json
function Classroom(teacher, students){ // 1 teacher, many student
    // start Classroom with Teacher
    teacher.setRole("Teacher");
    this.teacher = teacher;
    this.classroom = [teacher];
    // add each Student to Classroom
    this.students = students;
    this.students.forEach(student => { student.setRole("Student"); this.classroom.push(student); });
    // build json/string format of Classroom
    this.json = [];
    this.classroom.forEach(person => this.json.push(person.toJSON()));
}

// make a CompSci classroom from formerly defined teacher and students
compsci = new Classroom(teacher, students);

// output of Objects and JSON in CompSci classroom
logItType(compsci.classroom);  // constructed classroom object
logItType(compsci.classroom[0].name);  // abstract 1st objects name
logItType(compsci.json[0]);  // show json conversion of 1st object to string
logItType(JSON.parse(compsci.json[0]));  // show JSON.parse inverse of JSON.stringify
object ; [ Person { name: 'Mr M', ghID: 'jm1021', classOf: 1977, role: 'Teacher' },
  Person {
    name: 'Anthony',
    ghID: 'tonyhieu',
    classOf: 2022,
    role: 'Student' },
  Person { name: 'Bria', ghID: 'B-G101', classOf: 2023, role: 'Student' },
  Person { name: 'Allie', ghID: 'xiaoa0', classOf: 2023, role: 'Student' },
  Person {
    name: 'Tigran',
    ghID: 'Tigran7',
    classOf: 2023,
    role: 'Student' },
  Person {
    name: 'Rebecca',
    ghID: 'Rebecca-123',
    classOf: 2023,
    role: 'Student' } ]
string ; Mr M
string ; {"name":"Mr M","ghID":"jm1021","classOf":1977,"role":"Teacher"}
object ; { name: 'Mr M', ghID: 'jm1021', classOf: 1977, role: 'Teacher' }