Stage 3: Welcome to JavaScript
Data Types
  • Data Types

    JavaScript has several built-in data types that are used to represent different kinds of values. Understanding these data types is important when working with JavaScript code. Primitive Data Types: JavaScript has six primitive data types:

    1. Number: Used to represent numeric values, including integers and floating-point numbers.

    2. String: Used to represent text values.

    3. Boolean: Used to represent true/false values.

    4. Undefined: Used to represent a variable that has not been assigned a value.

    5. Null: Used to represent a variable that has been explicitly set to a null value.

    6. Symbol: Used to represent a unique identifier. Complex Data Types: JavaScript also has two complex data types:

    7. Object: Used to represent collections of key/value pairs.

    8. Function: Used to represent a block of code that can be executed.

    • String: A sequence of characters, such as "Hello World". Strings can be enclosed in either single or double quotes.

    • Number: Numeric values, including integers and floating-point numbers. For example, 10 and 3.14.

    • Boolean: Represents a true/false value. The values true and false are the only possible values for a Boolean.

    • Null: Represents a non-existent value. The value null represents a deliberate non-value.

    • Undefined: Represents a value that has not been assigned. The value undefined represents a value that has not been assigned or a declared variable that has not been assigned a value.

    • Object: A collection of properties and methods. Objects can be created using object literals or with the Object constructor.

    • Array: An ordered list of values. Arrays are objects and can contain elements of any data type.

    • Symbol :A unique and immutable data type that can be used as an identifier for object properties.

    • The typeof operator in JavaScript is used to determine the data type of a value. It returns a string that represents the data type of the operand. The syntax for using typeof is:

      typeof operand;

      For example:

      let name = 'John Doe';
      console.log(typeof name); // "string"
       
      let age = 30;
      console.log(typeof age); // "number"
       
      let isMarried = true;
      console.log(typeof isMarried); // "boolean"
       
      let car = null;
      console.log(typeof car); // "object"
       
      let job;
      console.log(typeof job); // "undefined"
    • It's important to note that in JavaScript, typeof null returns "object". This is considered a bug in the language and is a well-known issue.

    • The typeof operator can be useful for debugging and for checking the data type of a value before performing operations on it. It can also be used to determine if a variable has been declared and assigned a value, by checking if the result of typeof is "undefined". https://www.edureka.co/blog/wp-content/uploads/2012/09/data-types-in-python.png

      1. Video: "Different Data Types in JavaScript | JavaScript Tutorial | Learn JavaScript" https://www.youtube.com/watch?v=O9by2KcR2v4 (opens in a new tab)

      2. Article: "JavaScript Weekly: Data Types and Mutability" https://medium.com/launch-school/javascript-weekly-data-types-and-mutability-e41ab37f2f95 (opens in a new tab)