DEV Community

Cover image for Net Ninja 2
RyanEtten21127
RyanEtten21127

Posted on

Net Ninja 2

What is JavaScript?

JavaScript is a lightweight programming language that gets converted into machine language line by line and uses functions. It is a universal language used to design websites & apps, program robots, and much more.

There are a few things to note while coding in JavaScript. The information that is stored is outputted into a server, which is a machine that you request information to and it gives back a response.

The code that you put into JavaScript will not have an output unless you link the JavaScript to the HTML file. You can do this by linking the script to the end of the body tag.

<body>

    <script src="script.js"></script>
</body>
Enter fullscreen mode Exit fullscreen mode
  • Make sure that you link the JavaScript to the same name of the JavaScript file that you created.

Variables

Variables are containers for storing data values for later use. These variables are set usually with the commands let, const, and var.

let age = 25;
console.log(age); -----> 25
Enter fullscreen mode Exit fullscreen mode
  • Make sure to mark the end of a statement with a semicolon.

There are a few constraints when it comes to creating names for a variable:

  • Cannot have a space
    • Use camel case for two letter variables
let myAge = 25;
console.log(myAge); -------> 25
Enter fullscreen mode Exit fullscreen mode
  • Only letters, numbers, underscores, or dollar signs
    • However, it can’t start with a number
  • Can’t use words like “let”, “const”, and other key words to name variables

Data Types

The tools used to store information on JavaScript is called Data Types. There are a few basic data types that you need to remember when learning JavaScript:

  • Strings
  • Numbers
  • Booleans
  • Null
  • Undefined
  • Object
  • Symbol

Strings

Strings are just any series of characters used to form some sort of sentence or phrase.

console.log("Hello, World"); -----> Hello, World
Enter fullscreen mode Exit fullscreen mode

You can combine two strings together because of a method called string concatenation.

let firstName = 'John';
let lastName = 'Smith';

let fullName = firstName + ' ' + lastName;
console.log(fullName) = John Smith
Enter fullscreen mode Exit fullscreen mode
  • I created a space in between the firstName and lastName in order to create some space in between the two.

There are a few basic methods that I thought were pretty cool in customizing the output of the string:

  • .length - tells you the length of the string
    • JavaScript is a zero-based language, so it starts counting the characters from zero instead of one.
  • .toUpperCase - turns the whole output to uppercase letters
  • .toLowerCase - turns the whole output to lowercase letters
  • .indexOf("x") - tells you the position of a certain character (x)
  • .lastIndexOf("x") - tells you the last instance of a certain character (x)
  • .slice(x,y) - erases the result from "x" to "y"
  • .substr(x,y) - displays the result starting from x and continues for y characters
  • .replace("x", "y") - replaces a character (x) from one string into another character (y)
    • If there are multiple of the same characters being replaced, it just replaces the first instance of the character (.replaceAll replaces all instances)

Numbers

Numbers are simple; it is just any numerical value that allows you to perform any math operation.

let radius = 6;
console.log(radius); -----> 6
Enter fullscreen mode Exit fullscreen mode

There is no need to put a number inside of quotations because the system already recognizes it as a number. In fact, putting that number inside of quotations will make it a string rather than a number.

Just like in basic mathematics, there are math operators that you can use to get certain values:

  • addition (+)
  • subtraction (-)
  • multiplication (*)
  • division (/)
  • exponents (**)
  • remainder (%)
console.log(5 + 3); ------> 8
console.log(14 - 4); ------> 10
console.log(5 * 6); ------> 30
console.log(10 / 2); ------> 5
console.log(7**2); ------> 49
console.log(10 % 3); ------> 1
Enter fullscreen mode Exit fullscreen mode

If you try to do an operation with a non-numerical value, it will come out as NaN because it is a result of an impossible equation.

console.log(5 / 'hello'); ------> NaN
Enter fullscreen mode Exit fullscreen mode

Booleans

A Boolean is used to evaluate conditions in code. It yields in a result either true or false.

console.log(5 == 5); ------> true
Enter fullscreen mode Exit fullscreen mode

Similar to strings, there are some methods that you can use for booleans.

  • .includes - checks to see if the value includes the character given (If yes, output is true; If no, output is false)
  • != - not equal to
let name = "Ashley"

let result = name.includes("h"); ------> true
let result = name.includes("w"); ------> false

console.log(9 != 7); ------> true
console.log(9 != 9); ------> false
Enter fullscreen mode Exit fullscreen mode

Null & Undefined

Null and Undefined are really simple and there isn't really anything complicated related to it. Null is just a value that is specifically assigned to have no value. Undefined is a variable that has not been given a value yet.

let age = null;

console.log(age); -------> null
Enter fullscreen mode Exit fullscreen mode

Objects

Objects are just complex data structures. The most common type of object is an array. Arrays are used to store a collection of things in. For example, a collection of strings would look like this:

let names = "John", "Mark", "Samuel", "Alex";

console.log(names[0]); -----> John
console.log(names[1]); -----> Mark
console.log(names[2]); -----> Samuel
console.log(names[3]); -----> Alex
Enter fullscreen mode Exit fullscreen mode

There are also two pretty simple methods you can use for arrays:

  • .push - adds more values to an array
  • .pop - returns the last value in the set of strings

Type Conversion

Type Conversions is what it sounds like; it is used to convert data types.

let score = '100';

score = Number(score);
Enter fullscreen mode Exit fullscreen mode

You can check the type of a value by putting typeof in the console.log

console.log(typeof score); ------> number
Enter fullscreen mode Exit fullscreen mode

One drawback to type conversions is that you cannot change a string with non-numerical values into a number.

let result = Number('hello');

console.log(result); -------> NaN
Enter fullscreen mode Exit fullscreen mode

So, what is the point of JavaScript?

JavaScript is a crucial language of programming because without it, everything would feel so lifeless. In fact, every single application on a cellphone uses JavaScript. JavaScript is so much more than just coding that you put on a browser; it allows for communication and interaction. JavaScript seems like a really interesting language in coding, and I cannot wait to enhance my knowledge of Java.

Top comments (0)