Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>JS Basics: Type Checking and Comparisons</h2>
<i>Type Checking and Comparisons</i>
</br>
</br>
<i>(Please check your web inspector window)</i>
</br>
</br>
<i>(Note: "" will appear as an empty whitespace)</i>
</br>
</br>
<div id="here"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                window.onload = () => {
  const print = msg => {
    const br = document.createElement("br")
    const el = document.getElementById("here")
    const tn = document.createTextNode(msg)
    el.appendChild(tn)
    el.appendChild(br)
    console.log(msg)
  }
  
  const BROKEN = undefined
  const ALSO_BROKEN = null
  const NOT_BROKEN_BUT_EMPTY = ""
  
  //Test cases to demonstrate what happens
  const test = arg => {
    try {
      print(arg)
    } catch(ex) {
      print("Exception encountered: " + ex)
    }
  }
  
  test(BROKEN)
  test(ALSO_BROKEN)
  test(NOT_BROKEN_BUT_EMPTY)
  test("Hello")
  
  //undefined and null checking
  const testTwo = arg => {
    if (arg != undefined && arg != null) print(arg)
    else print("undefined or null caught: value unacceptable")
  }
  
  testTwo(BROKEN)
  testTwo(ALSO_BROKEN)
  testTwo(NOT_BROKEN_BUT_EMPTY)  
  testTwo("World")  
  
  //typeof to check basic data types 
  const NUM = 1
  const STR = "I AM A STRING"
  const OBJ = {}
  
  const testThree = arg => {
    if (typeof arg === 'string' || typeof arg === 'number') print(arg)
    else print("Not a string or a number")
  }
  
  testThree(NUM)
  testThree(STR)
  testThree(OBJ)
  
  //instanceof to check class or prototype membership
  const Person = function(name, age) {
    this.name = name
    this.age = age
  }
  const BOB = new Person("BOB", 42)
  
  const testFour = arg => {
    if (arg instanceof Person || arg.constructor === Person) print("I am a person")
    else print("Not a person")
  }
  
  testFour(BOB)
  testFour(NUM)
  testFour(STR)
  
  //comparison operations
  const NUM_NUM = 10
  //note that 10 (the number) and 10 (the string) will both be displayed as strings
  const STRING_NUM = "10"
  const FALSY = 0
  const FALSE = false
 
  const testFive = (argA, argB) => {
    if (argA == argB) print(`${argA} is loosely equal to ${argB}`)
    else print(`${argA} is not loosely equal to ${argB}`)
    
    if (argA === argB) print(`${argA} is strictly equal to ${argB}`)
    else print(`${argA} is not strictly equal to ${argB}`)
  }
  
  testFive(NUM_NUM, STRING_NUM)
  testFive(FALSY, FALSE)
}
              
            
!
999px

Console