Javascript -- Variables and Data Types



Comments  --   Variables  --   Data Types  --  

*** Testing SEO - How long it takes? Updated on 3.22.2024 5:47pm - LSB ***


Code


     console.log(100);
     console.log('Hello World');
     console.log(20, 'Hello', true);
     
     const x = 100;
     
     console.table({name: 'Lisa', age: 49, email: 'lisabertani1mom@gmail.com'});
     
     console.group('simple');
         console.log(x);
         console.error('Alert!');
         console.warn('Warning!');
     console.groupEnd();
     
     const styles = 'padding: 10px; background-color: black; color: red';
     
     // To convert and style into console %c - css 
     console.log('%cHello World', styles);
      
    

Console




Comments


    // This is a single line of code.

    /* This is a multi-line comment
    console.log(100);
    console.log('Hello World');
    console.log(20, 'Hello', true);
    */


Variables


3 ways to declare variables: var, let, const

Code


      let firstName = 'John';
      let lastName = 'Doe';

      console.log(firstName, lastName);

      let age = 27;

      console.log(age);


      // Naming conventions
      // - Only letters, numbers, underscores and dollar signs
      // - Can't start with a number

      /* Multi-Word Formatting
      firstName = camelCase - I will probably use this
      first_name = underscore - used in php
      FirstName = PascalCase - used in react
      firstname = lowercase - hard to read
      */

      // Re-assigning Variables
      age = 31;

      console.log(age);

      let score;

      score = 1;

      console.log(score);

      if (true) {
          score = score + 1;
      }

      console.log(score);

      // constants can not be re-assigned or 
      // just declared without value - must be initialized
      const x = 100;

      const arr = [1, 2, 3, 4];

      arr.push(5);

      console.log(arr);

      const person = {
          name: 'Brad',
      }

      person.name = 'John';

      person.email = 'brad@gmail.com';

      console.log(person);

      // Declare multiple values at once
      let a, b, c;

      const d = 10, e = 20, f = 30;

      console.log(d);
    

Console




















Data Types

Javascript has 2 types of data types: Primitive Data Types and Reference Types or Objects

Primitive Data Types

Primitive data types are stored directly in a "stack".

Reference Types (Objects)

Reference types or "objects" are a non-primitive value and when assigned to a variable, the variable is given a reference to that value

Reference types are stored in a heap and accessed by reference

Object literals, arrays and functions are all reference types

JavaScript is a dynamically type language. You do not have to put the type like string, number etc. You can change types as you go. Thats why it is dynamic not static.

Code


  // primitive values are stored on a stack
  const name = 'John';
  const age = '30';
  
  // reference values are stored on a heap
  const person = {
    name: 'Brad',
    age: 40  
  }
  
  let newName = name;
  newName = 'Johnathan';
  
  let newPerson = person;
  newPerson.name = 'Bradley';
  
  console.log(name, newName);
  console.log(person, newPerson);
  console.log(family);
    

Console