Skip to content
← Back to Community
JavaScript Functions, Arrays, and JSON
Profile icon
RowanFromBJC

Hello!

Today, I'm gonna teach you how to use some JavaScript functions. So, let's get into it!

Variables

You've probably used var before, if you've used JavaScript. Variables in JavaScript look like this:

var foo = 'bar'; console.log(foo); // Displays bar in the console

Or this (JSON)

var fooObj = {"foo": "bar"} console.log(fooObj.foo); // Displays bar in the console again

But, most developers say not to use var, unless you need to. So, use const and let as much as possible. Here is the difference.

// let { var foo = 'bar'; } console.log(foo); // Displays bar { let bar = 'foo'; console.log(bar); // Displays foo } console.log(bar) // bar is not defined
// const const foo = 'bar'; console.log(foo); // bar const foo = 'not foo'; // SyntaxError: Identifier 'foo' has already been declared

String

All string functions explained in one code block

var x = 'The terms foobar, foo, bar, and others are used as metasyntactic variables and placeholder names in computer programming or computer-related documentation. '; var y = 'They have been used to name entities such as variables, functions, and commands whose exact identity is unimportant and serve only to demonstrate a concept.'; console.log(x.charAt(0)); // "T" as in "The terms foobar, foo, bar..." console.log(x.concat(y)) // var x joined to var y console.log(x.indexOf('f')); // 10 because the first "f" is in the 10th place console.log(x.replace('The', 'Da')); // "Da terms foobar, foo, bar..." console.log(x.substring(9, 16)); // "foobar" console.log(x.toLowerCase()); // "the terms foobar, foo, bar..." Great for making things not case sensitive console.log(x.toUpperCase()); // "THE TERMS FOOBAR, FOO, BAR..."

Array

Arrays are pretty much lists. Here's how you create one and use it:

var array = [ 'foo', 'bar', 'foobar' ]; console.log(array); // ['foo', 'bar', 'foobar'] console.log(array[0]); // foo console.log(array[1]); // bar console.log(array[2]); // foobar array.pop(); console.log(array); // ['foo', 'bar'] array.push('foobar'); // ['foo', 'bar', 'foobar'] console.log(array); array.shift(); console.log(array); // ['bar', 'foobar'] array.unshift('foo'); // ['foo', 'bar', 'foobar']

JSON

JSON stands for JavaScript Object Notation. Here's a code block that explains it:

var obj = { "foo": "Bar", "fName": "Bobby", "lName": "Sue" } console.log(`${obj.foo} v1.0.0`); // Bar v1.0.0 console.log(`Your first name is ${obj.fName}`); // Your first name is Bobby console.log(`Your last name is ${obj.lName}`); // Your last name is Sue // Or, use a string. var objStr = `{ "foo": "Bar", "fName": "Bobby", "lName": "Sue" }` console.log(`${JSON.parse(objStr).foo} v1.0.0`); // Bar v1.0.0 console.log(`Your first name is ${JSON.parse(objStr).fName}`); // Your first name is Bobby console.log(`Your last name is ${JSON.parse(objStr).lName}`); // Your last name is Sue

And that's it!

Tell me in the comments if I missed anything or if there's any errors!

Voters
Profile icon
programmeruser
Profile icon
RowanFromBJC
Comments
hotnewtop
Profile icon
mohammedabergsson581

I like var better so no.